Principle of Inclusion and Exclusion (original) (raw)

Last Updated : 11 May, 2026

The Principle of Inclusion and Exclusion (PIE) is a counting technique used to find the size of the union of multiple sets by systematically including and excluding overlapping parts.

It corrects the overcounting that happens when you simply add the sizes of the sets. When sets have overlapping elements, counting them directly can lead to overcounting. The Principle of Inclusion and Exclusion fixes this by alternately adding and subtracting the sizes of the intersection to get the exact total.

inclusion

For example, when calculating the number of people who own a dog or a cat, PIE adds up all dog owners and subtracts all cat owners. This includes:

Formula for Principle of Inclusion & Exclusion

In general, for n sets A1, A2, . . ., An​:

|A_1 \cup A_2 \cup \dots \cup A_n| = \sum_{i=1}^{n} |A_i| - \sum_{1 \leq i < j \leq n} |A_i \cap A_j| + \sum_{1 \leq i < j < k \leq n} |A_i \cap A_j \cap A_k| - \dots + (-1)^{n+1} |A_1 \cap A_2 \cap \dots \cap A_n|

Where:

**Note: This formula effectively accounts for all overlaps among the sets, ensuring that each unique element is counted exactly once.

Formula for Two Sets

For two sets A and B

**∣AUB∣ = ∣A∣+∣B∣-∣A∩B∣

Where,

Formula for Three Sets

For three sets A,B, and C, the Principle of Inclusion and Exclusion (PIE) formula to find the size of the union is:

∣AUBUC∣ = ∣A∣+∣B∣+∣C∣-∣A∩B∣-∣A∩C∣-∣B∩C∣+∣A∩B∩C∣

Where,

Derivation of the Inclusion and Exclusion Principle Using Venn Diagrams

We can visualize this using a Venn diagram for a finite number of sets. Here we will discuss the formula for the Principle of Inclusion and Exclusion for two and three sets and derive it using a Venn diagram.

**Venn Diagram for Two Sets

Venn-Diagram-of-Two-Sets

Thus, the number of elements in the union of these two sets, ∣A ∪ and B∣, is given by:

∣A∪B∣ = ∣A∣ + ∣B∣ − ∣A∩B∣

Venn Diagram for Three Sets

Venn-Diagram-of-Three-Sets

Thus, the number of elements in the union of these three sets, ∣A∪B∪C∣, is given by:

∣A∪B∪C∣ = ∣A∣ + ∣B∣ + ∣C∣ − ∣A∩B∣ − ∣B∩C∣ − ∣C∩A∣ + ∣A∩B∩C∣

Applications in Computer Science

Principle of Inclusion and Exclusion (PIE) is used quite a lot in Computer Science, especially in areas that deal with counting, probability, and combinatorics.

Here are some key applications:

Solved Examples

**Question 1: In a class of 100 students:

How many students study either Math or Science?

**Solution:

As we know, n(A⋃B) = n(A) + n(B) – n(A⋂B)

**Given:

n(A⋃B) = n(A) + n(B) – n(A⋂B) = 60 + 45 - 20 = 85

Thus, 85 students study either Math or Science.

Question 2: The probability of getting at least one head is Problem: In a survey of 120 people:

How many people like either tea or coffee?

**Solution:

As we know, n(A⋃B) = n(A) + n(B) – n(A⋂B)

**Given:

Thus, n(A⋃B) = 80 + 70 - 50 = 100

Thus, 100 people like either tea or coffee.

**Question 3: A company has 30 employees, 10 are assigned to Task A, 15 to Task B, and 5 are assigned to both. How many employees are assigned to at least one task?

**Solution:

As we know, n(A⋃B) = n(A) + n(B) – n(A⋂B)

Given:

Substitute the Values, We get

n(A⋃B) = 10 + 15 - 5 = 20

Thus, 20 employees are assigned to at least one task.

**Question 4: In a survey of 200 people:

How many people like at least one of these three foods?

**Solution:

As we know, ∣AUBUC∣ = ∣A∣ + ∣B∣ + ∣C∣ - ∣A∩B∣ - ∣A∩C∣ - ∣B∩C∣ + ∣A∩B∩C∣

**Given:

Substituting in the formula, we get,

∣AUBUC∣ = 120 + 100 + 80 - 60 - 30 - 40 + 20 = 190

Thus, 190 people like at least one of the three foods.

**Question 5: In a university of 300 students:

How many students are taking at least one subject?

**Solution:

As we know, ∣AUBUC∣ = ∣A∣ + ∣B∣ + ∣C∣ - ∣A∩B∣ - ∣A∩C∣ - ∣B∩C∣ + ∣A∩B∩C∣

**Given:

Substituting values, we get

∣AUBUC∣ = 150 + 120 + 100 - 80 - 60 - 50 + 30 = 210

Thus, 210 students are taking at least one subject.

Practice Problems

**Question 1: In a group of 200 people:

How many people like either chocolate or vanilla?

**Question 2: In a class of 100 students:

How many students play at least one of these three sports?

**Question 3: Out of 150 attendees at an event:

How many people attended at least one workshop?

**Question 4: A card is drawn from a deck of 52 cards. What is the probability that it is:

There are 13 hearts, 12 face cards, and 26 red cards.

Note that:

**Question 5: In a school of 300 students:

How many students are enrolled in at least one subject?

**Answer Key