Skip to content
LeetQuant
F2 Counting and Combinatorics

F2 · Counting and Combinatorics · Lesson 1 of 4

Counting & Combinatorics

Permutations, combinations, stars and bars, inclusion–exclusion and symmetry arguments.

25 min

Count, then divide

When every outcome is equally likely, a probability is a ratio of counts: P(A)=∣A∣/∣Ω∣P(A) = |A| / |\Omega|. Most counting questions reduce to four building blocks.

SituationCount
Ordered, kk from nn, with repetitionnkn^k
Ordered, kk from nn, no repetitionn(n−1)⋯(n−k+1)=n!(n−k)!n(n-1)\cdots(n-k+1) = \dfrac{n!}{(n-k)!}
Unordered, kk from nn, no repetition(nk)=n!k! (n−k)!\dbinom{n}{k} = \dfrac{n!}{k!\,(n-k)!}
Unordered, kk from nn, with repetition(n+k−1k)\dbinom{n+k-1}{k}

The multiplication principle glues them together: if a choice is made in stages, and stage ii has mim_i options whatever happened before, the total is m1m2⋯m_1 m_2 \cdots.

Arranging nn objects where there are n1n_1 identical copies of one kind, n2n_2 of another and so on gives the multinomial coefficient n!n1! n2!⋯\dfrac{n!}{n_1!\,n_2!\cdots}.

Worked example

a full house

You are dealt 5 cards from a standard 52-card deck. What is the probability of a full house (three cards of one rank and two of another)?

Show solution

Build the hand in stages. Choose the rank of the triple (13 ways) and its three suits ((43)=4\binom43 = 4). Choose a different rank for the pair (12 ways) and its two suits ((42)=6\binom42 = 6):

13⋅4⋅12⋅6=3744.13 \cdot 4 \cdot 12 \cdot 6 = 3744 .

There are (525)=2,598,960\binom{52}{5} = 2{,}598{,}960 hands, so P=3744/2,598,960≈0.00144P = 3744 / 2{,}598{,}960 \approx 0.00144, about 1 in 694.

Note that the triple's rank and the pair's rank play different roles, so we use 13⋅1213 \cdot 12 rather than (132)\binom{13}{2}.

The complement is often easier

"At least one" questions are usually solved through "none". For nn people with uniform birthdays,

P(no shared birthday)=∏i=0n−1365−i365≈exp⁡ ⁣(−n(n−1)2⋅365),P(\text{no shared birthday}) = \prod_{i=0}^{n-1}\frac{365 - i}{365} \approx \exp\!\Big(-\frac{n(n-1)}{2 \cdot 365}\Big),

using 1−x≈e−x1 - x \approx e^{-x}. The exponent counts the (n2)\binom n2 pairs, each matching with probability 1365\tfrac{1}{365}. That is why matches appear so quickly: the number of pairs grows like n2n^2.

Worked example

someone shares your birthday

How many other people do you need before there is a better-than-even chance that at least one of them shares your birthday?

Show solution

Now each person matches a fixed day with probability 1365\tfrac{1}{365}, independently. We need 1−(364/365)n>121 - (364/365)^n > \tfrac12, i.e.

n>ln⁡2−ln⁡(364/365)≈0.6930.00274≈252.7,n > \frac{\ln 2}{-\ln(364/365)} \approx \frac{0.693}{0.00274} \approx 252.7 ,

so n=253n = 253. Compare this with the classic birthday problem: there, any of the (n2)\binom n2 pairs can match, so far fewer people are needed.

Stars and bars

The number of ways to write nn as an ordered sum of kk non-negative integers, x1+⋯+xk=nx_1 + \dots + x_k = n, is

(n+k−1k−1).\binom{n + k - 1}{k - 1}.

Picture nn stars and k−1k - 1 bars in a row: each arrangement splits the stars into kk groups. For positive integers, give each variable one star first, leaving (n−1k−1)\binom{n-1}{k-1}.

Stars and bars counts multisets, which are generally not equally likely. Three dice are 3 stars in 6 bins, so there are (85)=56\binom{8}{5} = 56 possible multisets of faces, but {1,1,1}\{1,1,1\} has probability 1216\tfrac{1}{216} while {1,2,3}\{1,2,3\} has 6216\tfrac{6}{216}. Use it to count, not to assign probabilities.

Inclusion–exclusion

For events A1,…,AnA_1, \dots, A_n,

P(⋃iAi)=∑iP(Ai)−∑i<jP(Ai∩Aj)+∑i<j<kP(Ai∩Aj∩Ak)−⋯P\Big(\bigcup_i A_i\Big) = \sum_i P(A_i) - \sum_{i<j} P(A_i \cap A_j) + \sum_{i<j<k} P(A_i \cap A_j \cap A_k) - \cdots

The classic application is derangements, permutations with no fixed point. With AiA_i = "element ii is fixed", the intersection of any kk of them has probability (n−k)!n!\tfrac{(n-k)!}{n!}, and there are (nk)\binom nk such intersections, so

P(no fixed point)=∑k=0n(−1)kk!  ⟶  1e≈0.368.P(\text{no fixed point}) = \sum_{k=0}^{n} \frac{(-1)^k}{k!} \;\longrightarrow\; \frac1e \approx 0.368 .

The count of derangements is Dn=n!∑k=0n(−1)k/k!D_n = n!\sum_{k=0}^n (-1)^k / k!, giving D3=2D_3 = 2, D4=9D_4 = 9, D5=44D_5 = 44.

Knowledge check

How many distinct arrangements are there of the letters in MISSISSIPPI?

Knowledge check

How many solutions does x+y+z=7x + y + z = 7 have in non-negative integers?

Knowledge check

Five people put their coats in a pile and each takes one back at random. What is the probability that nobody gets their own coat?

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).