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:
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.
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:
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:
There are \binom{n}{k} ways to choose a set S of size k, so:
Applying inclusion-exclusion. The number of permutations with at least one fixed point is:
where S_k = \binom{n}{k}(n-k)!. Simplifying:
So S_k = \frac{n!}{k!}, and:
The derangement formula. Subtracting from n!:
Factor out n!:
Derangement formula
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}.
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:
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.
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.
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.
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.
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.
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\%.
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
-
"D_n = n! - 1 (just remove the identity permutation)." This only removes the permutation where every element is fixed. Derangements require that no element is fixed. There are many permutations with some (but not all) fixed points, and those are not derangements either.
-
"D_1 = 1." There is no derangement of a single element — the only permutation of \{1\} is (1), which fixes element 1. So D_1 = 0.
-
"The probability of a derangement depends heavily on n." For n \geq 5, the probability is nearly constant at 1/e \approx 0.368. The convergence is so fast that the dependence on n is negligible in practice.
-
"Derangements have nothing to do with inclusion-exclusion." The derivation is a textbook application of inclusion-exclusion. The "bad" sets are A_i = \{\text{permutations fixing } i\}, and the derangement count is n! - |A_1 \cup \cdots \cup A_n|.
-
"To count permutations with exactly m fixed points, subtract D_{n-m} from n!." The correct formula is \binom{n}{m} \times D_{n-m}: first choose which m elements are fixed, then derange the remaining n - m.
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?):
-
Case A: \sigma(j) = 1. Elements 1 and j swap, and the remaining n - 2 elements must form a derangement among themselves. There are D_{n-2} such derangements.
-
Case B: \sigma(j) \neq 1. Element j cannot go to position j (derangement condition) and also does not go to position 1. Effectively, element j plays the role of element 1 in a derangement of n - 1 objects. There are D_{n-1} such derangements.
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:
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
- Inclusion-Exclusion Principle — the counting tool that derives the derangement formula.
- Permutations — Basics — the n! total permutations from which derangements are drawn.
- Factorial Notation — the notation n! and !n (subfactorial) used throughout this article.
- Permutations — Special Cases — permutations with restrictions, of which derangements are the most famous example.
- Combinations — Basics — the \binom{n}{m} factor that appears in counting permutations with exactly m fixed points.