In short

A derangement of n objects is a permutation in which no object remains in its original position. The number of derangements, denoted D_n (also written !n), is:

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)

For large n, this approaches n!/e. The derivation uses the inclusion-exclusion principle applied to fixed points.

Five friends — Aarav, Bhumi, Chirag, Divya, and Esha — each bring a gift to a party and put it in a common pile. Later, each person draws one gift at random. What is the probability that nobody gets their own gift back?

The total number of ways to distribute the five gifts is 5! = 120. The question reduces to: how many of those 120 permutations have the property that no person receives the gift they brought? Such a permutation is called a derangement, and counting derangements is one of the most elegant applications of inclusion-exclusion.

What is a derangement?

Derangement

A derangement of a set \{1, 2, \ldots, n\} is a permutation \sigma such that \sigma(i) \neq i for every i. In other words, no element is mapped to itself — there are no fixed points.

The number of derangements of n elements is denoted D_n or !n (the subfactorial of n).

For n = 1: the only permutation of \{1\} is (1), which is a fixed point. So D_1 = 0.

For n = 2: the permutations of \{1, 2\} are (1, 2) and (2, 1). Only (2, 1) has no fixed points. So D_2 = 1.

For n = 3: list all 3! = 6 permutations and check:

Permutation Fixed points? Derangement?
(1, 2, 3) All three fixed No
(1, 3, 2) 1 is fixed No
(2, 1, 3) 3 is fixed No
(2, 3, 1) None Yes
(3, 1, 2) None Yes
(3, 2, 1) 2 is fixed No

So D_3 = 2.

All 6 permutations of 3 elements, highlighting the 2 derangementsSix rows showing permutations of 1 2 3. Each row shows where each element maps to. Rows for (2,3,1) and (3,1,2) are highlighted in red as derangements. The other four rows show at least one element staying in place, marked with a circle. Permutations of {1, 2, 3} — derangements highlighted Position 1 2 3 Fixed? (1,2,3) 1→1 2→2 3→3 All 3 (1,3,2) 1→1 2→3 3→2 Pos 1 (2,1,3) 1→2 2→1 3→3 Pos 3 (2,3,1) 1→2 2→3 3→1 None ✓ (3,1,2) 1→3 2→1 3→2 None ✓ (3,2,1) 1→3 2→2 3→1 Pos 2 D₃ = 2 derangements out of 3! = 6 permutations
All $6$ permutations of $\{1, 2, 3\}$. The two highlighted rows are derangements — no element stays in its original position. The other four each have at least one fixed point.

Deriving the formula using inclusion-exclusion

The derivation is a direct application of the inclusion-exclusion principle to the "bad" events (fixed points).

Setup. Let A_i be the set of permutations of \{1, 2, \ldots, n\} that fix element i (i.e., \sigma(i) = i). A derangement is a permutation in none of A_1, A_2, \ldots, A_n. So:

D_n = n! - |A_1 \cup A_2 \cup \cdots \cup A_n|

Computing the intersection sizes. If a permutation fixes a specific set S of k elements (meaning \sigma(i) = i for all i \in S), the remaining n - k elements can be arranged in any order: (n - k)! permutations. So:

\left|\bigcap_{i \in S} A_i\right| = (n - k)! \qquad \text{for any } S \text{ with } |S| = k

There are \binom{n}{k} ways to choose a set S of size k, so:

S_k = \sum_{|S| = k}\left|\bigcap_{i \in S}A_i\right| = \binom{n}{k}(n-k)!

Applying inclusion-exclusion. The number of permutations with at least one fixed point is:

|A_1 \cup \cdots \cup A_n| = S_1 - S_2 + S_3 - \cdots + (-1)^{n+1}S_n

where S_k = \binom{n}{k}(n-k)!. Simplifying:

\binom{n}{k}(n-k)! = \frac{n!}{k!(n-k)!}\cdot(n-k)! = \frac{n!}{k!}

So S_k = \frac{n!}{k!}, and:

|A_1 \cup \cdots \cup A_n| = \frac{n!}{1!} - \frac{n!}{2!} + \frac{n!}{3!} - \cdots + (-1)^{n+1}\frac{n!}{n!}
Deriving Dn using inclusion-exclusionA flow showing the derivation. Top: Dn equals n factorial minus the size of the union of A1 through An. Middle: the union expands via inclusion-exclusion into alternating sums of Sk equals n factorial over k factorial. Bottom: factor out n factorial to get the final formula. Derivation of Dₙ Dₙ = n! − |A₁ ∪ A₂ ∪ … ∪ Aₙ| |A₁∪…∪Aₙ| = Σ n!/k! with alternating signs, k = 1 to n Dₙ = n! − n!/1! + n!/2! − n!/3! + … + (−1)ⁿ·n!/n! = n! × (1 − 1/1! + 1/2! − 1/3! + … + (−1)ⁿ/n!)
The derangement formula emerges from inclusion-exclusion. Each $S_k = \binom{n}{k}(n-k)!$ simplifies to $n!/k!$, and factoring out $n!$ from the alternating sum yields the clean formula.

The derangement formula. Subtracting from n!:

D_n = n! - \frac{n!}{1!} + \frac{n!}{2!} - \frac{n!}{3!} + \cdots + (-1)^n\frac{n!}{n!}

Factor out n!:

D_n = n!\left(1 - \frac{1}{1!} + \frac{1}{2!} - \frac{1}{3!} + \cdots + \frac{(-1)^n}{n!}\right) = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}

Derangement formula

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, D_n is the nearest integer to n!/e for all n \geq 1.

The first several values

n n! D_n D_n / n!
1 1 0 0
2 2 1 0.5
3 6 2 0.333\ldots
4 24 9 0.375
5 120 44 0.366\overline{6}
6 720 265 0.368055\ldots

The ratio D_n/n! oscillates and converges to 1/e \approx 0.367879\ldots. By n = 5, the ratio is already within 0.2\% of 1/e. This rapid convergence is because the sum \sum_{k=0}^n (-1)^k/k! is the partial sum of the Taylor series for e^{-1}.

Ratio of derangements to total permutations, converging to 1/eA plot with n on the horizontal axis from 1 to 8 and the ratio Dn over n factorial on the vertical axis from 0 to 0.5. Points alternate above and below the horizontal line at 1/e, converging rapidly. By n equals 5, the points are very close to 1/e. Dₙ/n! converges to 1/e ≈ 0.3679 0 0.5 0.25 1 2 3 4 5 6 n 1/e 0 0.5 0.333 0.375 0.367 0.368 n on horizontal axis, Dₙ/n! on vertical axis
The fraction $D_n/n!$ of permutations that are derangements converges rapidly to $1/e \approx 0.3679$. For $n = 1$, the ratio is $0$; for $n = 2$, it jumps to $0.5$; by $n = 5$ it settles near $1/e$. The oscillation (above, below, above...) reflects the alternating signs in the formula.

Subfactorial notation

The derangement count D_n is also written !n (read "subfactorial n"), placing the exclamation mark on the left instead of the right. This notation emphasises the parallel with n!: while n! counts all permutations, !n counts only the derangements.

A useful way to remember the formula: !n = n! \times (\text{partial sum of the series for } e^{-1}).

The "nearest integer" characterisation also gives a fast computation: calculate n!/e, round to the nearest integer, and that is D_n. For n = 5: 120/e \approx 120/2.71828 \approx 44.146, which rounds to 44 = D_5.

Solving the gift exchange problem

Return to the five friends with gifts. The number of ways nobody gets their own gift is D_5:

D_5 = 5!\left(1 - 1 + \frac{1}{2} - \frac{1}{6} + \frac{1}{24} - \frac{1}{120}\right) = 120\left(\frac{1}{2} - \frac{1}{6} + \frac{1}{24} - \frac{1}{120}\right)

Compute the sum inside the parentheses: \frac{60 - 20 + 5 - 1}{120} = \frac{44}{120}.

So D_5 = 120 \times \frac{44}{120} = 44.

The probability that nobody gets their own gift: \frac{D_5}{5!} = \frac{44}{120} = \frac{11}{30} \approx 36.7\%.

Remarkably, if the party had 50 friends instead of 5, the probability would still be approximately 1/e \approx 36.8\%. The number of people barely affects the probability — once n \geq 5, the fraction is essentially locked at 1/e.

Two worked examples

Example 1: Compute $D_4$ and list all derangements of $\{1, 2, 3, 4\}$.

Step 1. Apply the formula.

D_4 = 4!\left(1 - 1 + \frac{1}{2!} - \frac{1}{3!} + \frac{1}{4!}\right) = 24\left(\frac{1}{2} - \frac{1}{6} + \frac{1}{24}\right)

Why: the derangement formula with n = 4 has five terms (from k = 0 to k = 4). The first two terms (1 - 1) cancel, leaving three terms to evaluate.

Step 2. Compute the sum.

\frac{1}{2} - \frac{1}{6} + \frac{1}{24} = \frac{12 - 4 + 1}{24} = \frac{9}{24} = \frac{3}{8}

So D_4 = 24 \times \frac{3}{8} = 9.

Why: putting everything over the common denominator 24 makes the arithmetic clean. The result 9 means 9 out of 24 permutations of 4 elements are derangements.

Step 3. List all 9 derangements.

A derangement of \{1,2,3,4\} must satisfy: position 1 \neq 1, position 2 \neq 2, position 3 \neq 3, position 4 \neq 4.

(2,1,4,3), (2,3,4,1), (2,4,1,3), (3,1,4,2), (3,4,1,2), (3,4,2,1), (4,1,2,3), (4,3,1,2), (4,3,2,1)

Verify: in each, no element occupies its own position. Count: 9.

Why: listing confirms the formula. Systematic enumeration: position 1 can be 2, 3, or 4 (three choices), and for each, you check which completions avoid all fixed points.

Step 4. Cross-check with the nearest-integer method.

4!/e = 24/2.71828\ldots \approx 8.829, which rounds to 9. Matches.

Result. D_4 = 9. There are 9 derangements of 4 elements.

All 9 derangements of 4 elements shown as arrow diagramsNine small diagrams, each showing 4 positions connected to 4 values by arrows. In each diagram, no arrow connects position i to value i. Three derangements start with 2, three with 3, and three with 4. All 9 derangements of {1, 2, 3, 4} Starting with 2: (2, 1, 4, 3) (2, 3, 4, 1) (2, 4, 1, 3) Starting with 3: (3, 1, 4, 2) (3, 4, 1, 2) (3, 4, 2, 1) Starting with 4: (4, 1, 2, 3) (4, 3, 1, 2) (4, 3, 2, 1) D₄ = 9 out of 4! = 24 permutations Fraction: 9/24 = 3/8 = 0.375 (close to 1/e ≈ 0.368)
The $9$ derangements of $\{1,2,3,4\}$, grouped by the value in position $1$. Each group has $3$ derangements. Verify any one: in $(2,3,4,1)$, position $1$ holds $2$ (not $1$), position $2$ holds $3$ (not $2$), position $3$ holds $4$ (not $3$), position $4$ holds $1$ (not $4$).

The 3 + 3 + 3 = 9 derangements confirm the formula. The fraction 9/24 = 0.375 is already close to 1/e \approx 0.368.

Example 2: Six exam papers are randomly returned to six students. What is the probability that exactly $2$ students get their own paper?

Step 1. Reframe the problem.

Choose which 2 of the 6 students get their own paper: \binom{6}{2} ways. The remaining 4 papers must all go to the wrong student — a derangement of 4 elements.

Why: "exactly 2 fixed points" means picking which 2 positions are fixed and ensuring the other 4 are a derangement.

Step 2. Count the derangements of the remaining 4.

From Example 1, D_4 = 9.

Why: once the 2 correct papers are accounted for, the remaining 4 papers among 4 students must have zero fixed points — which is exactly the derangement condition.

Step 3. Multiply.

\text{Favourable outcomes} = \binom{6}{2} \times D_4 = 15 \times 9 = 135

Why: the choice of which 2 students are fixed (\binom{6}{2} = 15) is independent of the derangement of the remaining 4 (D_4 = 9). The multiplication principle applies.

Step 4. Compute the probability.

P(\text{exactly 2 fixed}) = \frac{135}{6!} = \frac{135}{720} = \frac{3}{16} = 0.1875

Why: the total number of ways to return 6 papers is 6! = 720, each equally likely. The ratio gives the probability.

Result. The probability that exactly 2 students get their own paper is \frac{135}{720} = \frac{3}{16} \approx 18.75\%.

Exactly 2 fixed points in a permutation of 6A row of 6 positions. Positions 2 and 5 are marked as fixed (paper returned correctly). The remaining 4 positions are connected by crossing arrows, representing a derangement. The calculation C(6,2) times D4 equals 15 times 9 equals 135 is shown below. 6 papers: exactly 2 returned correctly S₁ S₂ ✓ S₃ S₄ S₅ ✓ S₆ fixed fixed remaining 4: derangement Choose 2 fixed: C(6, 2) = 15 Derange remaining 4: D₄ = 9 15 × 9 = 135 → P = 135/720 = 3/16
One specific configuration: students $S_2$ and $S_5$ get their own papers (fixed points), while the other $4$ papers form a derangement. There are $\binom{6}{2} = 15$ ways to choose the fixed pair and $D_4 = 9$ derangements for the rest, giving $135$ favourable outcomes.

This "choose the fixed points, derange the rest" technique generalises to any question about permutations with exactly m fixed points. The count of permutations of n with exactly m fixed points is \binom{n}{m} \times D_{n-m}.

Common confusions

Going deeper

If you can compute D_n using the formula, apply the "choose fixed, derange the rest" technique, and understand the connection to 1/e, you are well-equipped for exam problems. The rest of this section covers the recursion and a combinatorial proof.

The recursion for D_n

Derangements satisfy two recursions:

Recursion 1: D_n = (n-1)(D_{n-1} + D_{n-2}) for n \geq 3, with D_1 = 0, D_2 = 1.

Why this works. Consider element 1. In a derangement, \sigma(1) = j for some j \neq 1. There are n - 1 choices for j. Now look at what happens to position j (where does element j go?):

Total: (n-1)(D_{n-1} + D_{n-2}).

Recursion 2: D_n = nD_{n-1} + (-1)^n for n \geq 2.

This follows from the closed-form formula. It gives a fast way to compute D_n from D_{n-1}: multiply by n and adjust by \pm 1.

From D_4 = 9: D_5 = 5 \times 9 + (-1)^5 = 45 - 1 = 44. From D_5 = 44: D_6 = 6 \times 44 + (-1)^6 = 264 + 1 = 265.

The hat-check problem

The derangement problem is classically called the "hat-check problem" or the probleme des rencontres (problem of coincidences): n guests check their hats, and the hats are returned at random. What is the probability that no guest gets their own hat? The answer, 1/e, has been known since the early 1700s. The rapid convergence to 1/e means that whether there are 5 guests or 500, the probability is nearly the same — an unintuitive but beautiful fact.

Derangements and the exponential function

The connection between D_n and e^{-1} is not a coincidence. The Taylor series for e^x evaluated at x = -1 gives:

e^{-1} = \sum_{k=0}^{\infty}\frac{(-1)^k}{k!} = 1 - 1 + \frac{1}{2} - \frac{1}{6} + \frac{1}{24} - \cdots

The derangement formula D_n = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!} is exactly n! times the n-th partial sum of this series. As n \to \infty, the partial sum approaches e^{-1}, so D_n/n! \to 1/e. The error in truncating the series at the n-th term is at most 1/(n+1)!, which is less than 1 for n \geq 0. This is why D_n is always the integer nearest to n!/e.

Where this leads next