๐Ÿงฎ Brain Teaser

The Hat Check Problem (Derangements)

Problem

At a party, nn people each check in their hat. The hats are returned in a completely random order. What is the probability that no one receives their own hat?

Find a closed form, and determine what happens as nโ†’โˆžn \to \infty.


Field

Probability / Combinatorics

Why It's Beautiful

The answer converges to 1/eโ‰ˆ36.8%1/e \approx 36.8\% incredibly fast โ€” for nโ‰ฅ5n \geq 5 it's already within 0.3%0.3\% of 1/e1/e. So whether you have 5 people or 5 million, the probability is essentially the same. It's remarkable that a purely combinatorial counting problem produces ee so naturally.

The key technique โ€” inclusion-exclusion โ€” is one of the most versatile tools in combinatorics and probability, and this is its most elegant showcase.

Key Idea / Trick

Let AiA_i = event that person ii gets their own hat. You want P(noneย ofย A1,โ€ฆ,An)P(\text{none of } A_1, \dots, A_n).

By inclusion-exclusion:

Pโ€‰โฃ(โ‹ƒAi)=โˆ‘k(โˆ’1)k+1(nk)(nโˆ’k)!n!=โˆ‘k=1n(โˆ’1)k+1k!P\!\left(\bigcup A_i\right) = \sum_k (-1)^{k+1} \binom{n}{k} \frac{(n-k)!}{n!} = \sum_{k=1}^n \frac{(-1)^{k+1}}{k!}

So P(derangement)=1โˆ’โˆ‘k=1n(โˆ’1)k+1k!=โˆ‘k=0n(โˆ’1)kk!โ†’eโˆ’1P(\text{derangement}) = 1 - \sum_{k=1}^n \frac{(-1)^{k+1}}{k!} = \sum_{k=0}^n \frac{(-1)^k}{k!} \to e^{-1}.

Difficulty

2 / 5

Tags

Probability, Combinatorics, Inclusion-exclusion, Derangements, ee, Permutations

DerangementsInclusion-exclusionPermutations

The Hat Check Problem โ€” Answer

Setup

A derangement is a permutation of {1,โ€ฆ,n}\{1, \dots, n\} with no fixed points. Let DnD_n = number of derangements. We want Pn=Dn/n!P_n = D_n / n!.


Solution via Inclusion-Exclusion

Let AiA_i = "person ii gets their own hat." We want P(noย Aiย occurs)P(\text{no } A_i \text{ occurs}).

Step 1. For any set SโІ{1,โ€ฆ,n}S \subseteq \{1,\dots,n\} with โˆฃSโˆฃ=k|S| = k: Pโ€‰โฃ(โ‹‚iโˆˆSAi)=(nโˆ’k)!n!P\!\left(\bigcap_{i \in S} A_i\right) = \frac{(n-k)!}{n!} since fixing kk people to their own hats leaves (nโˆ’k)!(n-k)! arrangements for the rest.

Pโ€‰โฃ(โ‹‚iโˆˆSAi)P\!\left(\bigcap_{i \in S} A_i\right) means the probability of kk people getting their own hats.

Step 2. By inclusion-exclusion:

The inclusion-exclusion principle expands P(โ‹ƒAi)P(\bigcup A_i) as: โˆ‘iP(Ai)โˆ’โˆ‘i<jP(AiโˆฉAj)+โˆ‘i<j<kP(AiโˆฉAjโˆฉAk)โˆ’โ‹ฏ\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

Grouping by the size kk of the intersection: there are (nk)\binom{n}{k} ways to choose which kk people all get their own hat, and from Step 1 each such event has probability (nโˆ’k)!/n!(n-k)!/n!. So:

Pโ€‰โฃ(โ‹ƒi=1nAi)=โˆ‘k=1n(โˆ’1)k+1(nk)โŸchooseย theย kย peopleโ‹…(nโˆ’k)!n!โŸprobย allย kย fixedP\!\left(\bigcup_{i=1}^n A_i\right) = \sum_{k=1}^n (-1)^{k+1} \underbrace{\binom{n}{k}}_{\text{choose the }k\text{ people}} \cdot \underbrace{\frac{(n-k)!}{n!}}_{\text{prob all }k\text{ fixed}}

The (nk)\binom{n}{k} and (nโˆ’k)!/n!(n-k)!/n! simplify neatly: (nk)โ‹…(nโˆ’k)!n!=n!k!โ€‰(nโˆ’k)!โ‹…(nโˆ’k)!n!=1k!\binom{n}{k} \cdot \frac{(n-k)!}{n!} = \frac{n!}{k!\,(n-k)!} \cdot \frac{(n-k)!}{n!} = \frac{1}{k!}

So the sum collapses to: Pโ€‰โฃ(โ‹ƒi=1nAi)=โˆ‘k=1n(โˆ’1)k+1k!P\!\left(\bigcup_{i=1}^n A_i\right) = \sum_{k=1}^n \frac{(-1)^{k+1}}{k!}

Step 3. The probability of a derangement is: Pn=1โˆ’Pโ€‰โฃ(โ‹ƒAi)=โˆ‘k=0n(โˆ’1)kk!P_n = 1 - P\!\left(\bigcup A_i\right) = \sum_{k=0}^n \frac{(-1)^k}{k!}

This is the partial sum of the Taylor series for exe^x at x=โˆ’1x = -1: Pn=โˆ‘k=0n(โˆ’1)kk!โ†’nโ†’โˆžeโˆ’1P_n = \sum_{k=0}^n \frac{(-1)^k}{k!} \xrightarrow{n\to\infty} e^{-1}


Closed Form for DnD_n

Dn=n!โˆ‘k=0n(โˆ’1)kk!=n!(1โˆ’1+12!โˆ’13!+โ‹ฏ+(โˆ’1)nn!)D_n = n! \sum_{k=0}^n \frac{(-1)^k}{k!} = n! \left(1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \cdots + \frac{(-1)^n}{n!}\right)

Equivalently, Dn=โŒŠn!/e+1/2โŒ‹D_n = \lfloor n!/e + 1/2 \rfloor โ€” the nearest integer to n!/en!/e.


Numerical Check

| nn | PnP_n | |-----|-------| | 1 | 0 | | 2 | 0.5 | | 3 | 0.333... | | 4 | 0.375 | | 5 | 0.3667 | | 10 | 0.36788... | | โˆž\infty | 1/eโ‰ˆ0.367881/e \approx 0.36788 |

The convergence is shockingly fast โ€” the alternating series error after nn terms is โ‰ค1/(n+1)!\leq 1/(n+1)!.


Why This Gives ee

The Taylor series ex=โˆ‘k=0โˆžxk/k!e^x = \sum_{k=0}^\infty x^k / k! evaluated at x=โˆ’1x = -1 gives exactly eโˆ’1e^{-1}. The inclusion-exclusion formula naturally produces the partial sums of this series. The connection is not a coincidence: the Poisson distribution with parameter ฮป=1\lambda = 1 assigns probability eโˆ’1โ‹…1k/k!e^{-1} \cdot 1^k / k! to kk events โ€” and a derangement corresponds to zero fixed points when fixed points are approximately Poisson(1).


Bonus: Recursion

Dn=(nโˆ’1)(Dnโˆ’1+Dnโˆ’2)D_n = (n-1)(D_{n-1} + D_{n-2}) with D1=0D_1 = 0, D2=1D_2 = 1.

Proof: Person 1 goes to position jโ‰ 1j \neq 1 ((nโˆ’1)(n-1) choices). Either person jj goes to position 1 (giving a derangement of the remaining nโˆ’2n-2, so Dnโˆ’2D_{n-2} ways), or person jj does not go to position 1 (equivalent to a derangement of nโˆ’1n-1 objects, so Dnโˆ’1D_{n-1} ways).

Related to Leetcode

This question appeared in the leetcode question 634.Find the Derangement-of-An-Array

See the solution here: solution

Type: ProbabilityEdit on GitHub โ†—