Skip to content
LeetQuant
F2 Counting and Combinatorics

F2 · Counting and Combinatorics · Lesson 2 of 4

Inclusion–Exclusion and Derangements

The general inclusion–exclusion formula and how to use it to count surjections, partial derangements and seatings with forbidden neighbours.

18 min

Why the signs alternate

Inclusion–exclusion counts objects that have none of a list of "bad" properties A1,…,AmA_1, \dots, A_m. Subtracting each ∣Ai∣|A_i| removes objects with two bad properties twice, so you add the pairwise intersections back. That puts objects with three properties back in too often, so you subtract triples, and so on.

The signs are exactly right because of one identity. Take an object with exactly r≥1r \ge 1 of the properties. In the alternating sum it is counted

1−(r1)+(r2)−⋯+(−1)r(rr)=(1−1)r=01 - \binom r1 + \binom r2 - \cdots + (-1)^r \binom rr = (1 - 1)^r = 0

times, and an object with no bad property is counted once (by the 11 term, the whole set). Every correct application comes down to this.

The formula

Let NN be the size of the universe and, for a set of indices II, let N(I)=∣⋂i∈IAi∣N(I) = \big|\bigcap_{i \in I} A_i\big| (with N(∅)=NN(\varnothing) = N). Define Sk=∑∣I∣=kN(I)S_k = \sum_{|I| = k} N(I). Then

#{objects with no property}=∑k=0m(−1)kSk,∣⋃iAi∣=∑k=1m(−1)k+1Sk.\#\{\text{objects with no property}\} = \sum_{k=0}^{m} (-1)^k S_k, \qquad \Big|\bigcup_i A_i\Big| = \sum_{k=1}^{m} (-1)^{k+1} S_k .

The formula is useful when the intersections are easy to count even though the union is not. The workhorse case is symmetric: if N(I)N(I) depends only on ∣I∣=k|I| = k, call it NkN_k, then Sk=(mk)NkS_k = \binom mk N_k and

#{none}=∑k=0m(−1)k(mk)Nk.\#\{\text{none}\} = \sum_{k=0}^{m} (-1)^k \binom mk N_k .

The interview recipe is always the same: (1) name the bad properties, (2) count what you get when you force a given set of kk of them to hold, (3) plug into the alternating sum.

Worked example

integers coprime to 30

How many integers in {1,…,1000}\{1, \dots, 1000\} are divisible by none of 2, 3 and 5?

Show solution

Bad properties: A2,A3,A5A_2, A_3, A_5 = "divisible by 2, 3, 5". Forcing a set of them means divisibility by their product, and the count of multiples of dd up to 1000 is ⌊1000/d⌋\lfloor 1000/d \rfloor:

kkintersectionsSkS_k
0everything10001000
1500+333+200500 + 333 + 20010331033
2⌊1000/6⌋+⌊1000/10⌋+⌊1000/15⌋=166+100+66\lfloor 1000/6 \rfloor + \lfloor 1000/10 \rfloor + \lfloor 1000/15 \rfloor = 166 + 100 + 66332332
3⌊1000/30⌋\lfloor 1000/30 \rfloor3333

So the answer is 1000−1033+332−33=2661000 - 1033 + 332 - 33 = 266.

Sanity check: the density of such integers is (1−12)(1−13)(1−15)=830(1 - \tfrac12)(1 - \tfrac13)(1 - \tfrac15) = \tfrac{8}{30}, and 1000⋅830≈266.71000 \cdot \tfrac{8}{30} \approx 266.7. The floors cause the small difference. (This product is exactly Euler's φ(n)/n\varphi(n)/n, which is inclusion–exclusion over the prime factors of nn.)

Surjections: every box gets something

How many functions from an nn-set onto a kk-set are there (every one of the kk targets is hit)? Let AjA_j = "target jj is missed". Forcing a given set of jj targets to be missed leaves (k−j)n(k - j)^n functions, so

Surj⁡(n,k)=∑j=0k(−1)j(kj)(k−j)n.\operatorname{Surj}(n, k) = \sum_{j=0}^{k} (-1)^j \binom kj (k - j)^n .

Dividing by k!k! removes the labels on the targets and gives the Stirling number of the second kind S(n,k)S(n, k), the number of ways to partition nn labelled items into kk non-empty unlabelled groups. For example, putting 5 distinct balls into 3 distinct boxes with none empty gives 35−3⋅25+3⋅15=1503^5 - 3 \cdot 2^5 + 3 \cdot 1^5 = 150 ways, and S(5,3)=150/6=25S(5, 3) = 150 / 6 = 25.

This is the "coupon collector after a fixed number of draws" question: P(all k types seen in n draws)=Surj⁡(n,k)/knP(\text{all } k \text{ types seen in } n \text{ draws}) = \operatorname{Surj}(n, k) / k^n.

Worked example

all six faces in ten rolls

You roll a fair die 10 times. What is the probability that every face appears at least once?

Show solution

With AjA_j = "face jj never appears", forcing jj given faces to be missing gives (6−j)10(6 - j)^{10} sequences. So the number of good sequences is

610−6⋅510+15⋅410−20⋅310+15⋅210−6⋅110+0=16,435,440.6^{10} - 6 \cdot 5^{10} + 15 \cdot 4^{10} - 20 \cdot 3^{10} + 15 \cdot 2^{10} - 6 \cdot 1^{10} + 0 = 16{,}435{,}440 .

Divide by 610=60,466,1766^{10} = 60{,}466{,}176:

P=16,435,44060,466,176=38,045139,968≈0.272.P = \frac{16{,}435{,}440}{60{,}466{,}176} = \frac{38{,}045}{139{,}968} \approx 0.272 .

A quick estimate from the first two terms (the Bonferroni bound) is 1−6(5/6)10≈1−0.969=0.0311 - 6(5/6)^{10} \approx 1 - 0.969 = 0.031, which is useless here: when the single-event probabilities are large, the higher-order terms matter. Compare the expected number of missing faces, 6(5/6)10≈0.976 (5/6)^{10} \approx 0.97; a Poisson approximation e−0.97≈0.38e^{-0.97} \approx 0.38 is only rough because the events are not rare.

Derangements, more carefully

The previous lesson derived Dn=n!∑k=0n(−1)k/k!D_n = n! \sum_{k=0}^{n} (-1)^k / k! for permutations with no fixed point. Three further facts come up constantly.

1. Nearest integer. The tail of the series for e−1e^{-1} after the k=nk = n term is alternating with decreasing terms, so ∣Dn−n!/e∣<1n+1≤12|D_n - n!/e| < \tfrac{1}{n+1} \le \tfrac12. Hence DnD_n is the nearest integer to n!/en!/e for every n≥1n \ge 1, and Dn/n!D_n / n! converges to 1/e1/e extremely fast: the error is below 1/(n+1)!1/(n+1)!.

2. Recurrence. Look at where element 1 goes, say to jj (n−1n - 1 choices). Either jj goes back to 1, leaving a derangement of the other n−2n - 2, or it does not, which is equivalent to deranging n−1n - 1 elements (treat "jj may not go to 1" as jj's forbidden spot). So

Dn=(n−1)(Dn−1+Dn−2),D0=1, D1=0,D_n = (n - 1)(D_{n-1} + D_{n-2}), \qquad D_0 = 1,\ D_1 = 0,

which also simplifies to Dn=nDn−1+(−1)nD_n = n D_{n-1} + (-1)^n. The sequence runs 1,0,1,2,9,44,265,1854,148331, 0, 1, 2, 9, 44, 265, 1854, 14833.

3. Exactly kk fixed points. Choose the fixed points, then derange the rest:

#{exactly k fixed}=(nk)Dn−k,P=1k!⋅Dn−k(n−k)!  →  e−1k!.\#\{\text{exactly } k \text{ fixed}\} = \binom nk D_{n-k}, \qquad P = \frac{1}{k!} \cdot \frac{D_{n-k}}{(n-k)!} \;\to\; \frac{e^{-1}}{k!} .

The number of fixed points of a random permutation is asymptotically Poisson(1). Consistent with that, its mean is exactly 1 for every nn (by linearity) and its variance is exactly 1 for every n≥2n \ge 2.

Worked example

exactly two hats returned

Six people check their hats and get them back in a uniformly random order. What is the probability that exactly two people get their own hat?

Show solution

Choose the two lucky people: (62)=15\binom62 = 15. The other four must all be wrong, which is a derangement of 4: D4=9D_4 = 9. So

P=15⋅96!=135720=316=0.1875.P = \frac{15 \cdot 9}{6!} = \frac{135}{720} = \frac{3}{16} = 0.1875 .

The Poisson limit gives e−1/2!≈0.184e^{-1}/2! \approx 0.184, already close at n=6n = 6.

Say the structure out loud in an interview: "choose who is fixed, derange the rest". It generalises to "exactly kk of the mm properties hold", where the count is ∑j≥k(−1)j−k(jk)Sj\sum_{j \ge k} (-1)^{j-k} \binom jk S_j.

Knowledge check

How many ways can 5 distinct balls be placed in 3 distinct boxes so that no box is empty?

Knowledge check

Seven letters are placed at random into their seven addressed envelopes, one per envelope. How many of the arrangements put every letter in a wrong envelope?

Knowledge check

For a uniformly random permutation of a large number of elements, what is the probability (to three decimals) that it has exactly one fixed point?

Key takeaways

  • Inclusion–exclusion works because an object with r≥1r \ge 1 bad properties is counted (1−1)r=0(1-1)^r = 0 times.
  • Recipe: name the bad properties, count the objects when a given kk of them are forced, alternate signs. In the symmetric case the answer is ∑k(−1)k(mk)Nk\sum_k (-1)^k \binom mk N_k.
  • Surjections onto kk targets: ∑j(−1)j(kj)(k−j)n\sum_j (-1)^j \binom kj (k-j)^n; divide by k!k! for Stirling numbers S(n,k)S(n,k).
  • DnD_n is the nearest integer to n!/en!/e; Dn=(n−1)(Dn−1+Dn−2)=nDn−1+(−1)nD_n = (n-1)(D_{n-1} + D_{n-2}) = n D_{n-1} + (-1)^n.
  • Exactly kk fixed points: (nk)Dn−k\binom nk D_{n-k}, asymptotically Poisson(1).
  • Truncated sums give alternating bounds; the first term is the union bound.

Practice problems

Finished reading?

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