Combinatorics
Intermediate
8 min

Inclusion-Exclusion

Count the size of a union of overlapping sets by adding and subtracting intersections systematically.

1. The idea

Inclusion-exclusion generalizes the two-set union formula to any number of sets: to count , sum the sizes of all single sets, subtract the sizes of all pairwise intersections, add back triple intersections, and so on, alternating signs.

It is the standard tool for 'at least one property holds' counting problems, and its complement form handles 'none of the properties hold,' which is often the more natural way to phrase constraints like 'no room is empty' or 'no letter is in its original position.'

A canonical application is counting surjections: the number of ways to assign distinguishable people to labeled rooms so that every room is non-empty uses inclusion-exclusion over the 'room is empty' events.

2. Why it matters

Inclusion-exclusion is the general-purpose tool behind derangements, surjection counts, and 'avoid this pattern' problems — recognizing when a direct count is contaminated by overlapping bad cases is a key interview skill.

3. Core mathematics

Two-set inclusion-exclusion

General inclusion-exclusion

Surjections from n elements onto k labeled sets

4. How to recognize it

Think of this when…

  • The problem asks to count outcomes where 'no room/box/person is empty' or 'at least one condition holds.'
  • You need to count elements satisfying at least one of several overlapping 'bad' properties.
  • A direct count would double-count overlaps between conditions.
  • The word 'surjection' or 'onto function' appears, or the setup describes it without naming it.

5. Worked example

Worked example

How many ways can 4 distinguishable people be assigned to 3 labeled rooms so that no room is empty?

  1. 1

    Total assignments without restriction: each of 4 people picks one of 3 rooms.

  2. 2

    Let = assignments where room is empty. By inclusion-exclusion, subtract single-empty-room counts, add back double-empty-room counts.

  3. 3

    Apply the surjection formula directly.

  4. Answer: 36 ways.

6. Common mistakes

  • Forgetting to alternate signs (add, subtract, add, ...) across successive intersection orders.
  • Missing higher-order intersection terms when more than two sets overlap.
  • Confusing 'surjection count' (rooms non-empty) with plain 'function count' (each person picks any room, no restriction).
  • Sign errors or forgetting the binomial coefficient weighting the number of ways to choose which sets intersect.

7. Quick check

How many integers from 1 to 100 are divisible by 2 or 5?

How many onto functions are there from a 3-element set to a 2-element labeled set?

8. Practice

5 QuantSolve questions use this concept.

Practice this concept

9. Related methods