Inclusion Exclusion principle for Competitive Programming
Last Updated :
23 Nov, 2023
What is the Inclusion-Exclusion Principle?
The inclusion-exclusion principle is a combinatoric way of computing the size of multiple intersecting sets or the probability of complex overlapping events.
Generalised Inclusion-Exclusion over Set:
For 2 Intersecting Set A and B:
- A\bigcup B= A + B - A\bigcap B
For 3 Intersecting Set A, B and C:
- A\bigcup B \bigcup C= A + B + C- A\bigcap B- A\bigcap C- B\bigcap C + - A\bigcap B \bigcap C
Inclusion Exclusion Principle on 3 overlapping sets.
For N Intersecting Sets: From above formulations we can clearly observe a pattern where odd combinations of set gets added while even Combination of Set gets Subtracted. This can be used to generalize our idea of inclusion and exclusion over a wide range of sets where:
- Get All combinations of sets i.e. all (2^N-1)
- Add the ones which have odd number of sets.
- Subtract the ones which have even number of sets.
Use Case of Inclusion Exclusion Principle:
1. Number of Divisors with certain conditions:
In competitive programming we can see many problems whose logic boils down to efficiently calculating multiples or divisors based on certain conditions. Let's take one such case,
Our Requirement: We need total numbers not greater than a given number 'N' and divisible by first 3 prime numbers={2,3,5}.
Brute Force Method: Simply iterate through 1 to N and check if the number is divisible by any of the first 3 prime number, if so increment the counter.
Solution Using Inclusion-Exclusion to solve in O(1): Using Inclusion Exclusion Principle this problem can be reduced to a simple mathematical equation, such that:
Numbers not greater than N and divisible by {2,3,5} =
Include(+) Numbers not greater than N and divisible by 2
Include(+) Numbers not greater than N and divisible by 3
Include(+) Numbers not greater than N and divisible by 5
Exclude(-) Numbers not greater than N and divisible by {2,3}
Exclude(-) Numbers not greater than N and divisible by {2,5}
Exclude(-) Numbers not greater than N and divisible by {3,5}
Include(+) Numbers not greater than N and divisible by {2,3,5}
Hence Result = N/2 + N/3 + N/5 - N/(2*3) - N/(2*5) - N/(3*5) + N/(2*3*5)
2. Inclusion-Exclusion with Combinatorics:
Combinatorics is one of the most widely used concept in competitive programming and it can be used to convert iterative computations to mathematical equations. Sometimes the required calculation may seem difficult to formulate, but using inclusion-exclusion with combinatorics we can calculate the required solution by Excluding the non-required solution from the total number of solutions. Let's understand this with example:
Example 1: Count of permutations of numbers [0,9] such that first element is greater than 1 and last element is smaller than 8.
Solution using Inclusion Exclusion: We can revert the problem using the principle such that instead of calculating the original problem we can do the following:
- Count of permutations of numbers [0,9] such that first element is smaller than equal to 1 and last element is greater than equal to 8
- Subtract the Count of Step 1, from the total number of permutations possible.
Let X= set of permutations in which the first element is <=1 => 2*9!
Let Y= set of permutations in which the last element is >=8 => 2*9!
Then X\bigcap Y = 2*2*8!
Then by Inclusion Exclusion Principle:
X\bigcup Y= X + Y - X\bigcap Y
= 2*9! + 2*9! + 2*2*8!
Subtract this count from the total number of permutations i.e. 10! to get the answer of the problem.
Example 2: Count how many subsequences of length 'N' exists consisting of only characters 'a', 'b' and 'c' such that each character occurs atleast once.
Solution using Inclusion Exclusion: Again we can change the framing of question such that we can calculate the count of such subsequences where 'a', 'b' and 'c' does not occur at all and subtract this count from the total number of the subsequnces possible (3^n)
Let A= number of subsequence where 'a' does not occur
Let B= number of subsequence where 'b' does not occur
Let C= number of subsequence where 'c' does not occur
Then using Inclusion-Exclusion Principle:
A\bigcup B\bigcup C=A+B+C-A\bigcap B-A\bigcap C-B\bigcap C + A\bigcap B\bigcap C
= 2^n + 2^n +2^n -1 -1 -1 + 0
Subtract this count from total number of subsequence possible i.e 3^n to get the answer.
3. Inclusion-Exclusion In 2-D prefix array:
Suppose we have a 2-D prefix array PRE[][] such that PRE[i][j] stores the information from 0'th row to i'th row and 0'th column to j'th column.
Our Requirement: Retrieve Information of the submatrice i1, j1 to i2, j2 as shown in the figure
.jpg)
Brute force: Iterate the submatrix to get the information which can take O(N*M) where N is the number of rows in the matrix and M is the number of columns in the matrix.
Solution using Inclusion Exclusion to solve in O(1): We know that PRE[i1][j1] will store the prefix information from (0,0) to (i1,j1) and PRE[i2][j2] will store the prefix information from (0,0) to (i2,j2) as shown below:
.jpg)
On applying Inclusion Exclusion principle we can get the information about the sub-matrix (i1,j1) to (i2,j2) as:
- PRE[i2][j2] – PRE[i2][j1-1] – PRE[i1-1][j2] + PRE[i1-1][j1-1]

Practice Problems on Inclusion Exclusion principle:
Similar Reads
Inclusion Exclusion principle for Competitive Programming
What is the Inclusion-Exclusion Principle?The inclusion-exclusion principle is a combinatoric way of computing the size of multiple intersecting sets or the probability of complex overlapping events. Generalised Inclusion-Exclusion over Set:For 2 Intersecting Set A and B: A\bigcup B= A + B - A\bigca
5 min read
Prefix Sum of Matrix (Or 2D Array)
Given a matrix (or 2D array) a[][] of integers, find the prefix sum matrix for it. Let prefix sum matrix be psa[][]. The value of psa[i][j] contains the sum of all values which are above it or on the left of it. Recommended PracticePrefix Sum of Matrix (Or 2D Array)Try It! Prerequisite: Prefix Sum -
12 min read
Count ways to create string of size N with given restrictions
Given a number N, the task is to count the number of ways to create a string of size N (only with capital alphabets) such that no vowel is between two consonants and the string does not start with the letter 'A' and does not end with the letter 'Z'. (Print the answer modulo 109 + 7). Examples: Input
15 min read
Count ways of selecting X red balls and Y blue balls
Given integers A, B, C, and D, There are two boxes First Box has A red balls and B blue balls and the second box has C red balls and D blue balls, the task is to count ways to select 3 red balls and 3 blue balls so that there are 3 balls drawn from the first box and 3 balls drawn from the second box
15+ min read
Count ways choosing K balls from any given A boxes
Given integers A and K, there are A boxes the first box contains K balls, the Second box contains K + 1 balls, the Third Box contains K + 3 balls so on till the A'th box contains K + A balls, the task for this problem is to count the number of ways of picking K balls from any of the boxes. (Print an
9 min read
Count number of ways in which following people can be arranged
Given integers X and Y representing X girls and Y boys, the task is to count the number of ways arranging X girls and Y boys such that girls always stay together and two boys from Y refuse to stay consecutive. Print the answer modulo 109 + 7. Examples: Input: X = 2, Y = 2Output: 4Explanation: Let's
6 min read
Count ways to choose Triplets of Pairs such that either first or second values are distinct
Given an array of pairs arr[] of size N (N ? 3) where each element of pair is at most N and each pair is unique, the task is to determine the number of ways to select triplets from the given N pairs that satisfy at least one of the following conditions: The first value (a) of each pair should be dis
7 min read