Sub topic 15
Inclusion-Exclusion for Overlapping Conditions
This chapter assumes you know the addition and multiplication principles, nCr and nPr, and the complement method from the chapter on selections with conditions. You should also be comfortable with union and intersection of sets.
Why plain addition overcounts
The addition principle says you can add case counts only when the cases cannot happen together. Many CAT questions use conditions that overlap. Suppose you count the integers from 1 to 30 divisible by 2 or by 3. There are 15 multiples of 2 and 10 multiples of 3. Adding gives 25. But 6, 12, 18, 24 and 30 sit in both lists, so each was counted twice. The true count is 25 − 5 = 20.
The inclusion-exclusion principle fixes this by adding the single sets, subtracting the overlaps, adding back what the subtraction removed too often, and so on.
The formulas
For two conditions A and B:
- |A ∪ B| = |A| + |B| − |A ∩ B|
For three conditions A, B and C:
- |A ∪ B ∪ C| = |A| + |B| + |C| − |A ∩ B| − |A ∩ C| − |B ∩ C| + |A ∩ B ∩ C|
The signs alternate with the number of sets in the intersection. Singles are added, pairs are subtracted, the triple is added, and a quadruple would be subtracted.
Worked example: numbers divisible by none of 2, 3, 5
How many integers from 1 to 100 are divisible by none of 2, 3 and 5?
Let A, B and C be the multiples of 2, 3 and 5. Use floor division for each count.
- Singles: 50 + 33 + 20 = 103.
- Pairs (multiples of 6, 10, 15): 16 + 10 + 6 = 32.
- Triple (multiples of 30): 3.
- Union: 103 − 32 + 3 = 74.
- None of the three: 100 − 74 = 26.
An intersection of "divisible by a" and "divisible by b" is "divisible by lcm(a, b)". For pairwise coprime numbers the lcm is the product.
Three standard CAT patterns
Pattern 1: distributions with no empty box
Distribute 5 distinct objects into 3 distinct boxes so that no box is empty. Let a "bad" event be that a particular box is empty.
- All distributions: 3^5 = 243.
- A specific box empty: 2^5 = 32, and there are 3 such boxes. Subtract 3 × 32 = 96.
- Two specific boxes empty: 1^5 = 1, and there are 3 pairs. Add 3.
- Result: 243 − 96 + 3 = 150.
The general form for n distinct objects into k distinct boxes with none empty is k^n − C(k,1)(k−1)^n + C(k,2)(k−2)^n − …
Pattern 2: forbidden positions
Four people, P1 to P4, stand in a row. P1 must not be first and P2 must not be second. Count the arrangements.
- Total: 4! = 24.
- P1 first: 3! = 6. P2 second: 3! = 6.
- Both: 2! = 2.
- Valid: 24 − 6 − 6 + 2 = 14.
This is the same idea that leads to derangements in the next chapter.
Pattern 3: upper bounds in stars and bars
Count the non-negative integer solutions of x + y + z = 10 with each variable at most 5.
- Unrestricted: C(12, 2) = 66.
- Violation of one variable, say x ≥ 6: put x = 6 + x′, so x′ + y + z = 4, which has C(6, 2) = 15 solutions. Three variables can violate, so subtract 3 × 15 = 45.
- Two variables violating needs a sum of at least 12, which exceeds 10, so this term is 0.
- Result: 66 − 45 = 21.
Always check whether the higher-order overlaps are possible. Often they are zero, and that saves time.
Verifying with code
Brute-force checks are a good way to confirm that you set up each term correctly.
from itertools import product, permutations
from math import comb
# Pattern 0: integers 1..100 divisible by none of 2, 3, 5
n = 100
formula = n - (n // 2 + n // 3 + n // 5) + (n // 6 + n // 10 + n // 15) - n // 30
brute = sum(1 for k in range(1, n + 1) if k % 2 and k % 3 and k % 5)
print(formula, brute) # 26 26
# Pattern 1: 5 distinct objects into 3 distinct boxes, none empty
formula = 3**5 - 3 * 2**5 + 3 * 1**5
brute = sum(1 for a in product(range(3), repeat=5) if len(set(a)) == 3)
print(formula, brute) # 150 150
# Pattern 2: 4 people, person 0 not first, person 1 not second
formula = 24 - 6 - 6 + 2
brute = sum(1 for p in permutations(range(4)) if p[0] != 0 and p[1] != 1)
print(formula, brute) # 14 14
# Pattern 3: x + y + z = 10, each in 0..5
formula = comb(12, 2) - 3 * comb(6, 2)
brute = sum(1 for x in range(6) for y in range(6) if 0 <= 10 - x - y <= 5)
print(formula, brute) # 21 21Common errors
- Wrong signs. The sign depends on how many sets are in the intersection: odd sizes get plus, even sizes get minus. Writing the formula out before filling in numbers prevents this.
- Mixing up "union" and "none". The formula gives the count of items meeting at least one condition. If the question asks for items meeting none, subtract the union from the total.
- Wrong overlap sizes. Do not multiply 6 by 10 to get the multiples of both. Use the lcm, which is 30.
- Missing terms. With three conditions there are 3 singles, 3 pairs and 1 triple. Count your terms before you evaluate them.
- Treating "exactly" as "at least". Inclusion-exclusion gives at-least-one counts. Exactly-one counts need extra work, such as subtracting twice the pair overlaps and three times the triple overlap.
Summary table
| Situation | Bad event | Typical term |
|---|---|---|
| Divisibility | Divisible by a given number | floor(N / lcm) |
| Onto distributions | A chosen box is empty | C(k, j)(k − j)^n |
| Forbidden positions | A chosen object sits in its forbidden place | (n − j)! |
| Bounded integer solutions | A chosen variable exceeds its limit | C(m + k − 1, k − 1) after shifting |
Exercise
How many integers from 1 to 200 are divisible by at least one of 3, 5 and 7?
Show answer
Singles: floor(200/3) + floor(200/5) + floor(200/7) = 66 + 40 + 28 = 134. Pairs: floor(200/15) + floor(200/21) + floor(200/35) = 13 + 9 + 5 = 27. Triple: floor(200/105) = 1. Union = 134 − 27 + 1 = 108.
Exercise
In how many ways can 4 distinct books be given to 2 distinct students so that each student gets at least one book?
Show answer
All distributions: 2^4 = 16. Subtract the cases where one student gets nothing: 2 × 1^4 = 2. Result: 16 − 2 = 14.
Exercise
Five people stand in a row. Person A must not be first and person B must not be last. How many arrangements are possible?
Show answer
Total 5! = 120. A first: 4! = 24. B last: 4! = 24. Both: 3! = 6. Valid: 120 − 24 − 24 + 6 = 78.