Open In App
Related Articles

Inclusion Exclusion principle for Competitive Programming

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

  1. 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
  2. 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

1-(1)

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:

2-(1)

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]
3

Practice Problems on Inclusion Exclusion principle:



Last Updated : 23 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads