Sub topic 16
Derangements: Nobody in Their Own Place
This chapter assumes you know factorials, nCr, and the inclusion-exclusion principle from the previous chapter. A derangement is an arrangement of n distinct objects in which no object sits in its original position. CAT questions dress this up as wrong letters in wrong envelopes, hats returned to the wrong owners, or students receiving someone else's answer sheet.
The Problem in Plain Terms
Suppose 4 letters go into 4 addressed envelopes. A derangement is a way to place every letter so that no letter lands in its own envelope. The count of derangements of n objects is written D(n), also called !n.
Small cases build intuition:
- n = 1: the only arrangement leaves the object in its place, so D(1) = 0.
- n = 2: swapping the two objects is the only valid arrangement, so D(2) = 1.
- n = 3: label the objects 1, 2, 3. The valid arrangements are 2 3 1 and 3 1 2, so D(3) = 2.
By convention D(0) = 1. There is exactly one way to arrange nothing, and it has no fixed object.
Deriving the Formula with Inclusion-Exclusion
Count all n! arrangements, then remove those where at least one object is fixed.
Let A_i be the set of arrangements in which object i is in its own place. Then:
- One object fixed: the other n-1 objects are free, so |A_i| = (n-1)!. There are C(n,1) such sets.
- Two objects fixed: |Ai ∩ Aj| = (n-2)!. There are C(n,2) such pairs.
- k objects fixed: (n-k)! arrangements for each of C(n,k) choices.
Inclusion-exclusion alternates the signs:
D(n) = n! − C(n,1)(n−1)! + C(n,2)(n−2)! − C(n,3)(n−3)! + ... + (−1)^n C(n,n)·0!
Since C(n,k)(n−k)! = n!/k!, this simplifies to:
D(n) = n! [1 − 1/1! + 1/2! − 1/3! + ... + (−1)^n / n!]
Check with n = 4: 24 × (1 − 1 + 1/2 − 1/6 + 1/24) = 24 − 24 + 12 − 4 + 1 = 9.
The Recurrence
The formula is slow to apply by hand for large n. A recurrence is faster:
D(n) = (n − 1) × [D(n−1) + D(n−2)]
The reasoning: object n can go to any of the other n−1 positions. Say it goes to position k. Then either object k goes to position n (a swap, leaving n−2 objects to derange) or it does not (which behaves like deranging n−1 objects). That gives D(n−1) + D(n−2) for each of the n−1 choices of k.
def derangements(n):
# D(0) = 1, D(1) = 0, D(n) = (n-1) * (D(n-1) + D(n-2))
d = [1, 0]
for i in range(2, n + 1):
d.append((i - 1) * (d[i - 1] + d[i - 2]))
return d[n]
print([derangements(n) for n in range(0, 7)]) # [1, 0, 1, 2, 9, 44, 265]There is also a one-step version: D(n) = n·D(n−1) + (−1)^n. For example D(5) = 5 × 9 − 1 = 44, and D(6) = 6 × 44 + 1 = 265.
Values to Memorize
| n | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| D(n) | 0 | 1 | 2 | 9 | 44 | 265 |
Learn these six numbers. Under exam time you rarely need to derive them, and most CAT questions use n ≤ 6.
Partial Derangements
A common variation asks for exactly r objects in the right place and the rest all wrong. Choose which r are correct, then derange the remaining n − r:
Count = C(n, r) × D(n − r)
Example: 6 letters go into 6 envelopes. In how many ways do exactly 2 letters reach the correct envelope?
- Choose the 2 correct letters: C(6,2) = 15.
- Derange the other 4: D(4) = 9.
- Total: 15 × 9 = 135.
The "exactly" matters. If the question says "at least 2 correct", you must add the cases for 2, 3, 4, and so on, or use the complement.
import math
def derangements(n):
d = [1, 0]
for i in range(2, n + 1):
d.append((i - 1) * (d[i - 1] + d[i - 2]))
return d[n]
def exactly_r_correct(n, r):
return math.comb(n, r) * derangements(n - r)
print(exactly_r_correct(6, 2)) # 135
print(sum(exactly_r_correct(5, r) for r in range(0, 6))) # 120, which is 5!The second line is a useful check. Splitting all arrangements by the number of correct placements must add up to n!.
Worked Examples
Example 1: No one gets their own hat
Five people leave hats at a party and each takes one hat at random. How many ways can everyone end up with a wrong hat?
This is D(5) = 44.
Example 2: At least one correct
Using the same five people, how many ways give at least one person their own hat?
Use the complement: 5! − D(5) = 120 − 44 = 76.
Example 3: Exactly one correct
Choose the one correct person in C(5,1) = 5 ways, then derange the other 4 in D(4) = 9 ways. Total 5 × 9 = 45.
Example 4: A restricted derangement with groups
Four boys and four girls each write their name on a slip. Each person must draw a slip of the opposite gender's name. This is not a standard derangement because the constraint is gender, not position. The answer is 4! × 4! = 576, since boys' slips can be assigned among the girls freely and vice versa. Always check whether the "forbidden" condition is really one forbidden place per object.
Common Errors
- Using n! − D(n) for "exactly one correct". That expression counts "at least one correct". Use C(n,1) × D(n−1) instead.
- Applying D(n) when only some objects have forbidden places. If only 2 of 5 letters have a forbidden envelope, use inclusion-exclusion on those 2 only, not D(5).
- Forgetting D(1) = 0 and D(2) = 1 in a chain of recurrence steps, which shifts every later value.
- Treating identical objects as distinct. Derangement formulas assume all n objects are different.
Quick Estimates
For large n the fraction of arrangements that are derangements approaches 1/e ≈ 0.368. So D(n) ≈ n!/e. The approximation is already exact after rounding for n ≥ 1. This helps you reject options in multiple-choice questions. For n = 5, 120/2.718 ≈ 44.15, which rounds to 44.
Exercise
Six students each submit a paper, and the papers are shuffled and handed back one per student. (a) In how many ways does no student get their own paper? (b) In how many ways does exactly one student get their own paper? (c) In how many ways does at least one student get their own paper?
Show answer
(a) D(6) = 265. (b) Choose the one student who gets their own paper in C(6,1) = 6 ways and derange the other 5 in D(5) = 44 ways, giving 6 × 44 = 264. (c) Use the complement: 6! − D(6) = 720 − 265 = 455.
Exercise
Use the recurrence D(n) = (n−1)[D(n−1) + D(n−2)] to compute D(7), given D(5) = 44 and D(6) = 265. Then verify it with D(n) = n·D(n−1) + (−1)^n.
Show answer
With the recurrence, D(7) = 6 × (265 + 44) = 6 × 309 = 1854. With the one-step form, D(7) = 7 × 265 + (−1)^7 = 1855 − 1 = 1854. Both agree.