The Wayback Machine - https://web.archive.org/web/20240728011648/https://www.geeksforgeeks.org/inclusion-exclusion-principle-for-competitive-programming/
Open In App

Inclusion Exclusion principle for Competitive Programming

Last Updated : 23 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
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:



Similar Reads

Inclusion Exclusion principle and programming applications
Sum Rule - If a task can be done in one of n1 ways or one of n2 ways, where none of the set of n1 ways is the same as any of the set of n2 ways, then there are n1 + n2 ways to do the task. The sum-rule mentioned above states that if there are multiple sets of ways of doing a task, there shouldn’t be any way that is common between two sets of ways b
13 min read
Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
This is a question asked quite often as in which language should be preferred to be efficient in competitive programming. It is something one should not worry about as what matters is the logic, not the language. Most of the languages are more or less same, but till now the most preferred language is C++, and here are the reasons. Python Simple and
5 min read
Competitive Programming vs General Programming
Programming enthusiasts face a crossroads - Competitive Programming or General Programming? This article sheds light on the distinctions between Competitive Programming vs General Programming, offering a quick guide for those navigating the coding landscape. Whether you're aiming for speed and precision in competitions or tackling real-world projec
3 min read
Dynamic Programming in Game Theory for Competitive Programming
In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive edge. Whether you're a seasoned coder or a newcomer
15+ min read
Top Programming Languages For Competitive Programming
Building an application, running a server, or even implementing a game needs a programming language as the foundation. There are almost more than 700 programming languages which are the most popular ones and this number will increase day by day. But, you don't need to learn all of them. Having a good command of anyone is enough for you to grow your
13 min read
Peterson's Algorithm for Mutual Exclusion | Set 1 (Basic C implementation)
Problem: Given 2 processes i and j, you need to write a program that can guarantee mutual exclusion between the two without any additional hardware support. Solution: There can be multiple ways to solve this problem, but most of them require additional hardware support. The simplest and the most popular way to do this is by using Peterson's Algorit
11 min read
Peterson's Algorithm for Mutual Exclusion | Set 2 (CPU Cycles and Memory Fence)
Problem: Given 2 process i and j, you need to write a program that can guarantee mutual exclusion between the two without any additional hardware support. We strongly recommend to refer below basic solution discussed in previous article. Peterson's Algorithm for Mutual Exclusion | Set 1We would be resolving 2 issues in the previous algorithm.  Wast
11 min read
Ad-hoc, Inclusion, Parametric &amp; Coercion Polymorphisms
When we talk about Polymorphism in C++, we come to hear the following four types: Discussing these in details: Ad-hoc Polymorphism, also called as Overloading Ad-hoc Polymorphism allows functions having same name to act differently for different types. For example: The + operator adds two integers and concatenates two strings. Above example could b
11 min read
How to read Competitive Programming Questions?
Competitive Programming is considered as a sport in the field of computer science. The culture of this sport is growing day by day and a lot of people are also considering this as a career choice. So, to help the participants with improving their efficiency in this sport, in this post, we have tried to cover all possible things, needed to keep in m
5 min read
Some useful C++ tricks for beginners in Competitive Programming
Here are some of the basic C++ tricks that every beginner in Competitive Programming should follow for increased speed. However, competitive programming can only be mastered with time and lots of practice. These tricks will just help you to save a little time during the contests which sometimes matters a lot. Using the auto keyword to declare data
3 min read
three90RightbarBannerImg