Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Inclusion-Exclusion






The Overlap Problem


The addition rule computes the size of a union by adding the sizes of its pieces — but only when those pieces are mutually exclusive. The moment two sets share elements, simply adding their sizes counts the shared elements twice.

The inclusion–exclusion principle is the systematic correction. Add the sizes of individual sets, subtract the sizes of pairwise intersections to remove the double-counting, add back the sizes of triple intersections to compensate for the over-correction, and continue with alternating signs until all intersections are accounted for.

This page builds the principle from the two-set case to the general formula, then turns to its complementary form — counting elements that belong to none of the listed sets. That complementary form produces the derangement count and other "none" results.



Two Sets

In a class of 30, 18 take French and 15 take Spanish. How many take at least one language? Adding the two lists seems the obvious way.
Two class lists, two numbers: put them together and count.
Adding gives 33 — more students than the class has.
countvaluewhat it counts
French list18everyone taking French
Spanish list15everyone taking Spanish
18 + 1533more than the 30 students in the class
on both lists7counted once in 18 and again in 15
33 − 726each student taking a language, once
The 77 students on both lists were counted twice. Taking them off once gives 2626, and the other 44 students take neither language.
Adding the sizes counts every element of the overlap twice; one subtraction puts it right.
Every element of A∪BA \cup B should be counted once on the left side. On the right, an element belonging to only AA is counted once (by ∣A∣|A|), an element belonging to only BB is counted once (by ∣B∣|B|), and an element in both is counted twice — once by ∣A∣|A| and once by ∣B∣|B|. Subtracting ∣A∩B∣|A \cap B| removes that extra count, restoring the correct total.
Each number in the calculation describes a different group of students. The 18 and the 15 are full class lists, and the 7 students who take both languages appear on each of them. The 26 is the number of different names across the two lists, which is what taking at least one of the two languages means. The 4 students who take neither language appear on no list at all.
The definition
The inclusion-exclusion principle counts a union by adding the sizes of the sets and correcting for the overlaps; for two sets, ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|.
For two sets AA and BB, the size of the union is
Inclusion-Exclusion Principle (Two Sets)
∣A∪B∣=∣A∣+∣B∣−∣A∩B∣.|A \cup B| = |A| + |B| - |A \cap B|.
Learn more about this formula: Inclusion-Exclusion Principle (Two Sets) →
The set symbols in the formula read naturally: ∪\cup is union, ∩\cap is intersection, and ∣A∣|A| is the number of elements in AA.
So the number of students taking at least one language is 18+15−7=2618 + 15 - 7 = 26.
It is a counting rule: include every set, exclude what was counted too often.
Example
In a group of 30 students, 18 take French and 15 take Spanish, with 7 taking both. The number taking at least one of the two languages is
18+15−7=26.18 + 15 - 7 = 26.
The remaining 4 students take neither.
30 students: 18 take French, 15 take Spanish, 7 take bothFrench F: 18Spanish S: 1511French only7bothin 18 andin 158Spanish onlyU: 30 students4 take neither|F| + |S| = 18 + 15 = 33 counts the 7 in both twice|F ∪ S| = 18 + 15 − 7 = 26 = 11 + 7 + 8
The overlap, counted twice

The example from the text, region by region: 11 students take only French, 8 only Spanish, 7 both, and 4 neither. The 18 French students include the 7, and so do the 15 Spanish students, so 18 + 15 = 33 counts those 7 twice. Subtracting |F ∩ S| once gives 26, the same as adding the three inner regions directly.

The 4 outside both circles are 30 - 26: the complementary count, taken up again in the complementary form below.
Adding the sizes counts the overlap twice; subtracting it once leaves every element counted exactly once.
Subtract the overlap once, not twice — and with three or more sets, the corrections keep alternating.
Pitfall: Subtracting the overlap twice
Writing 18+15−7−7=1918 + 15 - 7 - 7 = 19 drops the 7 students who take both languages altogether: 19 is the number taking exactly one (11+811 + 8). The 7 are counted twice in 18+1518 + 15, so subtracting them once gives 2626.
With three sets, subtracting the pairwise overlaps takes the triple overlap away too often, so it has to be added back, as three sets shows.
The rest of the page covers three sets, the general formula, the complementary form, counting derangements, and worked examples.

Three Sets


For three sets, the same logic extends with one more correction:

Inclusion-Exclusion Principle (Three Sets)
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|.
Learn more about this formula: Inclusion-Exclusion Principle (Three Sets) →


An element belonging to exactly one of the three sets is counted once on the right. An element in exactly two is counted twice by the singles and subtracted once by its single pairwise intersection — net count 11. An element in all three is counted three times by the singles, subtracted three times by the pairwise intersections, and added back once by the triple intersection — net count 3−3+1=13 - 3 + 1 = 1. Every element ends up counted exactly once.

Example
Among the integers from 1 to 100, the number divisible by 2, 3, or 5 is computed by applying the three-set formula to the divisibility sets. With ∣A∣=50|A| = 50 (multiples of 2), ∣B∣=33|B| = 33 (multiples of 3), ∣C∣=20|C| = 20 (multiples of 5), and the pairwise and triple intersections counting multiples of 66, 1010, 1515, and 3030:
50+33+20−16−10−6+3=74.50 + 33 + 20 - 16 - 10 - 6 + 3 = 74.

Pitfall: Forgetting the triple intersection
Stopping after the pairs gives 50+33+20−16−10−6=7150 + 33 + 20 - 16 - 10 - 6 = 71: the multiples of 30 are added three times and subtracted three times, so they are not counted at all. Adding ∣A∩B∩C∣=3|A \cap B \cap C| = 3 back gives 7474.

Each term in the example is a count of multiples. Among the integers from 1 to 100 there are 1616 multiples of 6, 1010 multiples of 10 and 66 multiples of 15, one count for each pair of primes, and 33 multiples of 30, the numbers 30, 60 and 90 that are divisible by all three primes. The seven terms of the formula use each of these counts once, with a sign set by how many of the sets meet in that term.
ABC111181618141303
After the three subtractions

Each region shows how many times its elements have been counted so far (large number) and how many elements it holds (small number). The three singles counted the centre three times; the three pairwise subtractions each took it back once, so the centre now reads 0 while every other region reads 1. Adding |A ∩ B ∩ C| back is the last correction. Step through all seven terms on the inclusion-exclusion explorer.

Add the triple intersection back and every region reads exactly 1: each element of the union is counted once.
The same example, the integers from 1 to 100 with AA, BB, CC = multiples of 22, 33, 55, one term at a time. Each region shows how many integers it holds and how many times they have been counted so far.
DownloadPNG2×PDF
running total0
ABC26 outside27×014×013×07×07×03×03×0
Times each element is counted
×0not counted
×1counted once
×2counted twice
×3counted three times
Click a region to see which terms of the formula count it. Click a term, or use the arrows, to add the formula one term at a time.
0 of 7 terms added.
Three sets, one term at a time·/combinatorics/inclusion-exclusionLearn Math Class

The General Formula


For nn sets A1,A2,…,AnA_1, A_2, \ldots, A_n:

The General Formula
∣⋃i=1nAi∣=∑i∣Ai∣−∑i<j∣Ai∩Aj∣+∑i<j<k∣Ai∩Aj∩Ak∣−⋯+(−1)n+1∣A1∩A2∩⋯∩An∣.\left| \bigcup_{i=1}^{n} A_i \right| = \sum_{i} |A_i| - \sum_{i<j} |A_i \cap A_j| + \sum_{i<j<k} |A_i \cap A_j \cap A_k| - \cdots + (-1)^{n+1} |A_1 \cap A_2 \cap \cdots \cap A_n|.

In compact form,

Inclusion-Exclusion Principle (General)
∣⋃i=1nAi∣=∑k=1n(−1)k+1∑1≤i1<i2<⋯<ik≤n∣Ai1∩Ai2∩⋯∩Aik∣.\left| \bigcup_{i=1}^{n} A_i \right| = \sum_{k=1}^{n} (-1)^{k+1} \sum_{1 \le i_1 < i_2 < \cdots < i_k \le n} |A_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_k}|.
Learn more about this formula: Inclusion-Exclusion Principle (General) →


Pitfall: Giving the last term a plus sign
With n=4n = 4, an element in all four sets is counted 4−6+4+1=34 - 6 + 4 + 1 = 3 times when the four-fold term is added. Its sign is (−1)4+1=−1(-1)^{4+1} = -1, and 4−6+4−1=14 - 6 + 4 - 1 = 1.

The sign of each level depends only on how many sets meet in its intersections. Single sets enter with a plus, pairs with a minus, triples with a plus, and the pattern continues up to the one term that intersects all nn sets. In the compact form this is the factor (−1)k+1(-1)^{k+1}, which is +1+1 when kk is odd and −1-1 when kk is even; the expanded form writes the same factor as (−1)n+1(-1)^{n+1} on its last term.

Structure of the Formula


The number of terms at level kk — meaning the number of kk-fold intersections being summed — is the binomial coefficient (nk)\binom{n}{k}, one term for each kk-subset of the nn sets. The total number of terms across all levels is

The General Formula
∑k=1n(nk)=2n−1,\sum_{k=1}^{n} \binom{n}{k} = 2^n - 1,

which is the number of non-empty subsets of the nn sets. Every non-empty subset contributes exactly one term to the formula.

Why the Alternating Signs Work


An element belonging to exactly mm of the nn sets is counted

The General Formula
(m1)−(m2)+(m3)−⋯+(−1)m+1(mm)\binom{m}{1} - \binom{m}{2} + \binom{m}{3} - \cdots + (-1)^{m+1} \binom{m}{m}

times by the right-hand side. By the binomial theorem with a=1a = 1 and b=−1b = -1,

The General Formula
∑k=0m(−1)k(mk)=0,\sum_{k=0}^{m} (-1)^k \binom{m}{k} = 0,

so (m0)=1\binom{m}{0} = 1 minus the alternating sum from k=1k=1 to mm gives 11. Every element in the union is counted exactly once, as required.
The counting argument above as a chain: how often the formula counts one element, and why that number is always 11.
DownloadPNG2×PDF
ShowEvery element in the union is counted exactly once
1
(m1)−(m2)+(m3)−⋯+(−1)m+1(mm)\binom{m}{1} - \binom{m}{2} + \binom{m}{3} - \cdots + (-1)^{m+1} \binom{m}{m}
countThe number of times the right-hand side counts an element belonging to exactly mm of the nn sets
2
∑k=0m(−1)k(mk)=0\sum_{k=0}^{m} (-1)^k \binom{m}{k} = 0
key stepThe binomial theorem with a=1a = 1 and b=−1b = -1
3
(m0)=1\binom{m}{0} = 1
rearrangeSplit off the k=0k = 0 term: (m0)=1\binom{m}{0} = 1 minus the alternating sum from k=1k = 1 to mm gives 11
Every element in the union is counted exactly once
Why the alternating signs workLearn Math Class
The binomial theorem with a=1a = 1 and b=−1b = -1 is the key step: it turns the alternating count of a single element into exactly 11.
Fact: Every cut-off point is a bound
Stopping after an odd level always overcounts the union, and stopping after an even level always undercounts it. After level kk, an element in mm sets has been counted 1+(m−1k)1 + \binom{m-1}{k} times if kk is odd and 1−(m−1k)1 - \binom{m-1}{k} times if kk is even.

With many sets the full formula is long: ten sets already give 210−1=10232^{10} - 1 = 1023 terms. In practice the sum is often cut off after the first few levels, keeping only the singles, or the singles and the pairs, and dropping the rest. For the integers from 1 to 100 and the primes 2, 3 and 5, the singles alone give 50+33+20=10350 + 33 + 20 = 103, the singles and the pairs give 103−32=71103 - 32 = 71, and only the full sum gives the true count 7474.

The Complementary Form


Often the question is not how many elements belong to some of the AiA_i, but how many belong to none of them. If UU is a universal set containing all candidate elements, the elements outside every AiA_i form the intersection of the complements:

Inclusion-Exclusion Complementary Form
∣A1‾∩A2‾∩⋯∩An‾∣=∣U∣−∣A1∪A2∪⋯∪An∣.\left| \overline{A_1} \cap \overline{A_2} \cap \cdots \cap \overline{A_n} \right| = |U| - \left| A_1 \cup A_2 \cup \cdots \cup A_n \right|.
Learn more about this formula: Inclusion-Exclusion Complementary Form →


Expanding the right side using the general formula gives a direct expression for the count of elements satisfying none of the conditions defined by A1,…,AnA_1, \ldots, A_n:

The Complementary Form
∣U∣−∑i∣Ai∣+∑i<j∣Ai∩Aj∣−∑i<j<k∣Ai∩Aj∩Ak∣+⋯+(−1)n∣A1∩⋯∩An∣.|U| - \sum_{i} |A_i| + \sum_{i<j} |A_i \cap A_j| - \sum_{i<j<k} |A_i \cap A_j \cap A_k| + \cdots + (-1)^n |A_1 \cap \cdots \cap A_n|.

This is the natural form for problems phrased as "how many elements avoid all of the following conditions" — derangements, surjective functions, arrangements with forbidden positions, and divisibility problems requiring relative primality to several primes.

Pitfall: Subtracting the sets one by one
Taking 30−18−15=−330 - 18 - 15 = -3 for the students who take neither language removes the 7 who take both twice. Adding the overlap back gives 30−18−15+7=430 - 18 - 15 + 7 = 4.

The language example from the two-set section is the smallest case. The universal set UU is the class of 30 students, A1A_1 is the set of students taking French and A2A_2 the set taking Spanish. The students who avoid both conditions lie outside A1∪A2A_1 \cup A_2, and with n=2n = 2 the complementary form has four terms: ∣U∣|U|, ∣A1∣|A_1|, ∣A2∣|A_2| and ∣A1∩A2∣|A_1 \cap A_2|.
UAB
Outside every set

The shaded region is everything in U that lies in neither A nor B: the universe with the union removed, which is also the overlap of the two complements. Its size is |U| − |A ∪ B|, so counting the elements that avoid every condition comes down to one union count. Compare it with the other two-set regions on the two-set Venn diagram tool.

With more sets the picture is the same: the region outside all the circles, found by subtracting the union from U.

Application: Counting Derangements


A derangement of {1,2,…,n}\{1, 2, \ldots, n\} is a permutation in which no element appears in its original position. The derangement formula is derived by applying the complementary form of inclusion–exclusion to the set of all permutations.

Setup


For each ii, let AiA_i denote the set of permutations of {1,…,n}\{1, \ldots, n\} in which position ii is fixed (the element ii remains in its original position). The derangements are the permutations belonging to none of A1,A2,…,AnA_1, A_2, \ldots, A_n.

Intersection Sizes


The size of any kk-fold intersection Ai1∩Ai2∩⋯∩AikA_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_k} equals (n−k)!(n-k)! — fixing kk specific positions leaves the remaining n−kn-k positions to be permuted freely. The size depends only on kk, not on which kk positions are chosen.

Derivation


The total number of permutations is n!n!. Applying the complementary form of inclusion–exclusion:

Application: Counting Derangements
!n=n!−(n1)(n−1)!+(n2)(n−2)!−⋯+(−1)n(nn)⋅0!.!n = n! - \binom{n}{1}(n-1)! + \binom{n}{2}(n-2)! - \cdots + (-1)^n \binom{n}{n} \cdot 0!.

Simplifying each term using (nk)(n−k)!=n!k!\binom{n}{k}(n-k)! = \frac{n!}{k!} gives the explicit derangement formula:

Derangement
!n=n!∑k=0n(−1)kk!.!n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}.
Learn more about this formula: Derangement →


The permutations page states this formula and applies it. The derivation above is what justifies it.
The derivation above in five steps, one at a time; use Next and Back to move through it.
DownloadPNG2×PDF
Derive!n=n!∑k=0n(−1)kk!!n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}
1 / 5
Name the sets to avoid
A1,A2,…,AnA_1, A_2, \ldots, A_n
For each ii, AiA_i is the set of permutations in which position ii is fixed. The derangements are the permutations belonging to none of A1,A2,…,AnA_1, A_2, \ldots, A_n.
Deriving the derangement formulaLearn Math Class
The intersection count in the second step does the work: multiplied by (nk)\binom{n}{k}, it simplifies to n!k!\frac{n!}{k!} in every term.
The six permutations of 1, 2, 3 and how many of the sets A1, A2, A3 each one is inposition123in how many Aᵢperm. 11233perm. 21321perm. 32131perm. 42310perm. 53120perm. 63211element i in position i: the permutation is in Aᵢ!3 = 3!(1 − 1 + 1/2 − 1/6) = 2In none of A₁, A₂, A₃: 6 − 3·2! + 3·1! − 0! = 2
Permutations of 1, 2, 3 and the sets Aᵢ

A permutation is in Aᵢ when element i stays in position i, so the last column is how many of the sets it belongs to. The identity is in all three, three permutations are in exactly one, and two are in none. Adding the column gives 6 = 3·2!, the sum of the |Aᵢ|, and the complementary form removes everything that is in at least one set, leaving the 2 derangements.

Each k-fold intersection has (3 − k)! permutations, which is why the terms are 3!, 3·2!, 3·1! and 0!.
When every k-fold intersection has the same size, inclusion–exclusion shrinks to a single sum over k.

Worked Examples


Three standard problem types beyond derangements.

Divisibility


How many integers from 1 to 100 are divisible by none of 2, 3, or 5?

Let AiA_i be the set of integers divisible by the ii-th prime in the list. From the three-set example above, ∣A∪B∪C∣=74|A \cup B \cup C| = 74. The complementary count is

Worked Examples
100−74=26.100 - 74 = 26.

These are the integers from 1 to 100 that share no prime factor with 30.

Surjective Functions


How many functions from a set of size nn to a set of size kk are surjective (every element of the codomain has at least one preimage)?

Fact: A sum of powers that equals k!k!
When n=kn = k, a surjection between two sets of the same size is a bijection, so the sum below must equal k!k!: for k=3k = 3 it is 27−24+3−0=627 - 24 + 3 - 0 = 6. When n<kn < k no surjection exists, and the same alternating sum of powers is exactly 00.

For each element ii of the codomain, let AiA_i be the set of functions that miss ii — that is, no element of the domain maps to ii. A surjective function avoids every AiA_i. The size of any jj-fold intersection Ai1∩⋯∩AijA_{i_1} \cap \cdots \cap A_{i_j} is (k−j)n(k-j)^n, since the domain elements may map freely to the remaining k−jk-j codomain elements. The total count of functions is knk^n. By inclusion–exclusion:

Surjective Functions Count
#{surjections [n]→[k]}=∑j=0k(−1)j(kj)(k−j)n.\#\{\text{surjections } [n] \to [k]\} = \sum_{j=0}^{k} (-1)^j \binom{k}{j} (k-j)^n.
Learn more about this formula: Surjective Functions Count →


Pitfall: Swapping n and k in the total
Writing the number of functions as 43=644^3 = 64 for n=4n = 4, k=3k = 3 counts maps from the 3-set into the 4-set. Each of the 4 domain elements has 3 possible images, so there are 34=813^4 = 81 functions, and the sum starts from 8181.

For a concrete case, take a domain of n=4n = 4 elements and a codomain of k=3k = 3 elements. The sum then has four terms, for j=0,1,2,3j = 0, 1, 2, 3, and gives 81−48+3−0=3681 - 48 + 3 - 0 = 36. These 3636 surjections are the functions that use every one of the three codomain elements at least once. The term for j=3j = 3 is 00, because no function can miss the whole codomain.

Arrangements With Forbidden Positions


In how many ways can nn items be arranged so that none of mm specified items occupies its own forbidden position? Define AiA_i (i=1,…,mi = 1, \ldots, m) as the set of arrangements where item ii does occupy its forbidden position. The complementary count, computed via inclusion–exclusion on the AiA_i, gives the answer. The derangement formula is the special case where each of the nn items has exactly one forbidden position and the forbidden positions are all distinct.
DownloadPNG2×PDF
Layout
Problem typeWhat to countDefine Aᵢ asFinal formula
Divisibility / coprimalityintegers in 1..N divisible by none of given primesintegers in 1..N divisible by the i-th primeN − |A₁ ∪ A₂ ∪ … ∪ Aₙ|
Surjective functionssurjections from an n-set to a k-setfunctions missing codomain element i; |j-fold intersection| = (k − j)ⁿΣⱼ₌₀ᵏ (−1)j^{j} C(k, j) (k − j)ⁿ
Derangementspermutations of 1..n with no fixed pointpermutations fixing position i; |k-fold intersection| = (n − k)!!n = n! · Σₖ₌₀ⁿ (−1)ᵏ ⁄ k!
Forbidden positionsarrangements of n items avoiding specified forbidden positionsarrangements with item i in its forbidden positionn! − |A₁ ∪ … ∪ Aₙ| via inclusion–exclusion (derangements are the special case)
Worked ExamplesLearn Math Class

Common Pitfalls

Five mistakes come up again and again when inclusion–exclusion is applied. Each is marked where it happens in the sections above; here each is worked through in full, with the wrong and the right working side by side.
DownloadPNG2×PDF
1Subtracting the overlap twice
The overlap is removed from each of the two sets before they are added, as if both ∣A∣|A| and ∣B∣|B| had to be cleaned of it. The elements in both sets then drop out of the count completely.
Wrong turn
Error here
With 18 students taking French, 15 taking Spanish and 7 taking both, the number taking at least one language is given as 18+15−7−7=1918 + 15 - 7 - 7 = 19.
Right path
18+1518 + 15 counts the 7 twice, once on each list, so one subtraction leaves them counted once: 18+15−7=2618 + 15 - 7 = 26. By regions: 1111 take only French, 88 only Spanish and 77 both, and 11+8+7=2611 + 8 + 7 = 26.
Why it happensThe count for "only AA", ∣A∣−∣A∩B∣|A| - |A \cap B|, is mixed up with the count for the union. Subtracting the overlap from each set gives the elements in exactly one set, which answers a different question.
How to checkAdd up the regions. Every element of the union is in exactly one of "only AA", "only BB" and "both", so the three region counts must add up to the answer; here they give 2626, not 1919.
2Forgetting the triple intersection
For three sets, the singles and the pairs are written down and the count stops there. The elements in all three sets are then added three times and subtracted three times, so they are lost.
Wrong turn
Error here
The integers from 1 to 100 divisible by 2, 3 or 5 are counted as 50+33+20−16−10−6=7150 + 33 + 20 - 16 - 10 - 6 = 71.
Right path
Add back the 33 multiples of 30: 50+33+20−16−10−6+3=7450 + 33 + 20 - 16 - 10 - 6 + 3 = 74.
Why it happensThe two-set formula ends with a subtraction, and subtracting the overlaps feels like the complete correction. The over-correction at the centre stays hidden until one element is traced through every term.
How to checkTrace an element that is in all three sets, such as 3030. It is counted by ∣A∣|A|, ∣B∣|B| and ∣C∣|C| and removed by all three pairwise terms, so without the triple term it is counted 3−3=03 - 3 = 0 times.
3Giving the last term a plus sign
For four or more sets, the term that intersects all nn sets is added, as in the three-set formula. Its sign is (−1)n+1(-1)^{n+1}, which is a minus whenever nn is even.
Wrong turn
Error here
For four sets, an element in all four is counted (41)−(42)+(43)+(44)=4−6+4+1=3\binom{4}{1} - \binom{4}{2} + \binom{4}{3} + \binom{4}{4} = 4 - 6 + 4 + 1 = 3 times.
Right path
The four-fold term has the sign (−1)4+1=−1(-1)^{4+1} = -1, so the element is counted 4−6+4−1=14 - 6 + 4 - 1 = 1 time, as required.
Why it happensThe three-set formula is the one most often memorized, and it ends with a plus. The habit carries over to larger nn.
How to checkApply the formula to nn copies of the same one-element set. Their union has 11 element, so the signed sum must come out as 11; with a wrong last sign it does not.
4Subtracting the sets one by one
To count the elements in none of the sets, each set is subtracted from the universal set separately. Elements that belong to more than one set are then removed more than once.
Wrong turn
Error here
The students taking neither French nor Spanish are counted as 30−18−15=−330 - 18 - 15 = -3.
Right path
Subtract the union, not the separate sets: 30−(18+15−7)=30−18−15+7=430 - (18 + 15 - 7) = 30 - 18 - 15 + 7 = 4.
Why it happens"not French and not Spanish" sounds like two separate removals, and the overlap is easy to forget once the question is about the outside of the sets.
How to checkThe answer can be at most the size of the smallest complement, here 30−18=1230 - 18 = 12, and it can never be negative. With other numbers the wrong answer may land inside these limits, so the reliable habit is to compute the union first and then subtract it from ∣U∣|U|.
5Swapping n and k in the total
In the surjection count, the number of all functions from an nn-set to a kk-set is written as nkn^k instead of knk^n, and the whole sum is built on the wrong total.
Start
For n=4n = 4 and k=3k = 3,
Wrong turn
Error here
The number of functions is given as 43=644^3 = 64. That is the number of functions from a 3-element set into a 4-element set.
Right path
Each of the 44 domain elements picks one of 33 images independently, so there are 3⋅3⋅3⋅3=34=813 \cdot 3 \cdot 3 \cdot 3 = 3^4 = 81 functions. The sum is 81−3⋅24+3⋅14−04=81−48+3−0=3681 - 3 \cdot 2^4 + 3 \cdot 1^4 - 0^4 = 81 - 48 + 3 - 0 = 36.
Why it happensBoth expressions are one size raised to the other, and knk^n is remembered as a pattern without the choices behind it.
How to checkBuild the count as a product of choices, one factor for each domain element; the number of factors is the exponent. For a single domain element mapped into 3 elements there are 3=313 = 3^1 functions, not 13=11^3 = 1.
Common pitfalls with inclusion–exclusion·/combinatorics/inclusion-exclusionLearn Math Class
Four of the five slips break the pattern of the formula: the overlap is subtracted once, the triple intersection is added back, the signs alternate all the way to the last term, and the union is found before it is subtracted from the total. The fifth swaps base and exponent in the total knk^n, which has one factor for each of the nn domain elements. Adding up the regions, tracing one element through every term, or building the count as a product of choices catches each slip.

Related Concepts


• Set Theory — unions, intersections, complements, and the cardinality identities are the formal structure inclusion–exclusion operates on.

• Probability — the probabilistic version of inclusion–exclusion is the standard tool for computing the probability of a union of non-disjoint events.

Inclusion–Exclusion at a Glance

This summary sets out the five forms of inclusion-exclusion from the page, one card each. Every card names the form, states its formula, says when to use it and links to the section that explains it, with a worked example underneath.

The two-set and three-set forms correct the double counting of overlapping groups, the second adding back the triple overlap. The general form handles any number of sets with alternating signs and has one term for each non-empty group of the sets. The complementary form counts what lies outside every set, and the last card applies it to derangements, the orders in which no item keeps its place, whose share of all orders comes close to one in e.
DownloadPNG2×PDF
Two sets
∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|
For two overlapping groups: the double count is corrected once, as two sets shows.
18 French plus 15 Spanish minus 7 taking both: 18+15−7=2618 + 15 - 7 = 26
Three sets
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|
Singles minus pairs plus the triple: with three overlapping conditions, one over-correction is added back (three sets).
multiples of 22, 33 or 55 in 11 to 100100: 7474
n sets, union form
∣⋃i=1nAi∣\displaystyle \left| \bigcup_{i=1}^{n} A_i \right| =∑k=1n(−1)k+1\displaystyle = \sum_{k=1}^{n} (-1)^{k+1} ∑1≤i1<⋯<ik≤n∣Ai1∩⋯∩Aik∣\displaystyle \sum_{1 \le i_1 < \cdots < i_k \le n} |A_{i_1} \cap \cdots \cap A_{i_k}|
The general count of a union of nn overlapping sets, with 2n−12^n - 1 terms in total, from the general formula.
any union problem with explicit intersection sizes
n sets, complementary form
∣U∣−∑i∣Ai∣+∑i<j∣Ai∩Aj∣−∑i<j<k∣Ai∩Aj∩Ak∣+⋯+(−1)n∣A1∩⋯∩An∣\displaystyle |U| - \sum_{i} |A_i| + \sum_{i<j} |A_i \cap A_j| - \sum_{i<j<k} |A_i \cap A_j \cap A_k| + \cdots + (-1)^n |A_1 \cap \cdots \cap A_n|
Counts the elements that satisfy none of the conditions, for problems that ask to avoid all forbidden cases (the complementary form).
integers in 11 to 100100 coprime to 3030: 2626
Derangement specialization
!n=n!∑k=0n(−1)kk!≈n!e\displaystyle !n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!} \approx \frac{n!}{e}
No item lands in its own original position; counting derangements derives it from the complementary form.
shuffled hats: the chance that no one gets their own hat tends to 1/e1/e
Inclusion-exclusion formulas·All combinatorics formulasLearn Math Class
Every form rests on one check: an element that lies in several sets must end up counted exactly once. When a problem asks for none, for avoiding every condition or for no item in its place, start from the complementary form; when it asks for at least one, the union form answers directly.

Skills You Should Learn

Each skill opens its step-by-step method page. Skills marked "On this page" are taught above.
Taught on this page
More combinatorics methods
  • Use complementary countingCount the outcomes you do not want and subtract them from the total; the standard route for "at least one" questions.

Inclusion-Exclusion FAQ

Why do you subtract the overlap when counting two sets?

+
Because adding the two totals counts everything in both sets twice, once from each. Subtracting the overlap once removes the duplicate and leaves each item counted exactly once. When the sets share nothing there is no overlap to remove and the two totals simply add.Read more →

When should you use the complementary form instead?

+
When the question asks how many items satisfy none of the conditions. Counting the total and subtracting everything that satisfies at least one is usually shorter than counting the exceptions directly. Problems phrased with none, no, or not any are the signal that this version fits better.Read more →

What happens if you stop inclusion-exclusion early?

+
You get a bound instead of the answer. Stopping after an odd level, such as the singles, can only overcount the union, and stopping after an even level, such as the pairs, can only undercount it. For the integers from 1 to 100 divisible by 2, 3 or 5, the singles give 103103, the singles and pairs give 7171, and only the full sum gives 7474. The error comes only from elements lying in more sets than the last level kept.Read more →

How many terms does the inclusion-exclusion formula have?

+
2n−12^n - 1 for nn sets, one for each non-empty subset of the sets. Level kk has (nk)\binom{n}{k} terms, one per kk-fold intersection, so three sets give 3+3+1=73 + 3 + 1 = 7 terms and ten sets already give 10231023. The complementary form adds one more term, ∣U∣|U|, for 2n2^n in all.Read more →

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

+
3636. Of the 34=813^4 = 81 functions, inclusion-exclusion removes those that miss some element of the codomain: 81−(31)24+(32)14−(33)04=81−48+3−0=3681 - \binom{3}{1} 2^4 + \binom{3}{2} 1^4 - \binom{3}{3} 0^4 = 81 - 48 + 3 - 0 = 36. The general count is ∑j=0k(−1)j(kj)(k−j)n\sum_{j=0}^{k} (-1)^j \binom{k}{j} (k-j)^n. With n=kn = k it gives k!k!, the bijections, and with n<kn < k it gives 00.Read more →