Open In App

Inclusion-Exclusion and its various Applications

In the field of Combinatorics, it is a counting method used to compute the cardinality of the union set. According to basic Inclusion-Exclusion principle
 




 

Principle :


Inclusion-Exclusion principle says that for any number of finite sets , Union of the sets is given by = Sum of sizes of all single sets – Sum of all 2-set intersections + Sum of all the 3-set intersections – Sum of all 4-set intersections .. + Sum of all the i-set intersections. 
In general it can be said that, 
 




Properties : 
 

  1. Computes the total number of elements that satisfy at least one of several properties.
  2. It prevents the problem of double counting.


Example 1: 
As shown in the diagram, 3 finite sets A, B and C with their corresponding values are given. Compute 
 


Solution : 
The values of the corresponding regions, as can be noted from the diagram are – 


By applying Inclusion-Exclusion principle, 


 

Applications :


 

Approach : – Inclusion-Exclusion Principle is a combinatorial counting technique that allows us to count the number of elements in the union of multiple sets. The principle states that the size of the union of two or more sets is equal to the sum of their sizes minus the size of their intersection, plus the size of the intersection of their pairwise intersections, and so on.

Here’s the step-by-step approach in C++ to implement the Inclusion-Exclusion Principle:

   Define the sets that need to be combined.

   Compute the size of each set.

   Compute the size of each intersection of two sets.

   Compute the size of each intersection of three sets.

   Continue computing the size of each intersection of four, five, and so on sets until you reach the final intersection.

   Sum the sizes of all sets.

   Subtract the size of all pairwise intersections.

   Add the size of all three-way intersections.

   Continue adding and subtracting the intersections of increasing sizes until you reach the final intersection.

   Return the final count.

Here is an example of implementing the Inclusion-Exclusion Principle in C++ to find the number of positive integers less than 100 that are divisible by either 2, 3, or 5:

#include <iostream>
using namespace std;
 
int main() {
    int n = 100;
    int count = 0;
     
    // Count the number of integers divisible by 2
    for(int i = 2; i < n; i += 2) {
        count++;
    }
     
    // Count the number of integers divisible by 3
    for(int i = 3; i < n; i += 3) {
        count++;
    }
     
    // Count the number of integers divisible by 5
    for(int i = 5; i < n; i += 5) {
        count++;
    }
     
    // Count the number of integers divisible by both 2 and 3
    for(int i = 6; i < n; i += 6) {
        count--;
    }
     
    // Count the number of integers divisible by both 2 and 5
    for(int i = 10; i < n; i += 10) {
        count--;
    }
     
    // Count the number of integers divisible by both 3 and 5
    for(int i = 15; i < n; i += 15) {
        count--;
    }
     
    // Count the number of integers divisible by 2, 3, and 5
    for(int i = 30; i < n; i += 30) {
        count++;
    }
     
    // Print the final count
    cout << "The number of positive integers less than " << n << " that are divisible by either 2, 3, or 5 is " << count << "." << endl;
     
    return 0;
}

                    
n = 100
count = 0
 
# Count the number of integers divisible by 2
for i in range(2, n, 2):
    count += 1
 
# Count the number of integers divisible by 3
for i in range(3, n, 3):
    count += 1
 
# Count the number of integers divisible by 5
for i in range(5, n, 5):
    count += 1
 
# Count the number of integers divisible by both 2 and 3
for i in range(6, n, 6):
    count -= 1
 
# Count the number of integers divisible by both 2 and 5
for i in range(10, n, 10):
    count -= 1
 
# Count the number of integers divisible by both 3 and 5
for i in range(15, n, 15):
    count -= 1
 
# Count the number of integers divisible by 2, 3, and 5
for i in range(30, n, 30):
    count += 1
 
# Print the final count
print(f"The number of positive integers less than {n} that are divisible by either 2, 3, or 5 is {count}.")

                    
public class Main {
    public static void main(String[] args) {
        int n = 100;
        int count = 0;
 
        // Count the number of integers divisible by 2
        for (int i = 2; i < n; i += 2) {
            count++;
        }
 
        // Count the number of integers divisible by 3
        for (int i = 3; i < n; i += 3) {
            count++;
        }
 
        // Count the number of integers divisible by 5
        for (int i = 5; i < n; i += 5) {
            count++;
        }
 
        // Count the number of integers divisible by both 2 and 3
        for (int i = 6; i < n; i += 6) {
            count--;
        }
 
        // Count the number of integers divisible by both 2 and 5
        for (int i = 10; i < n; i += 10) {
            count--;
        }
 
        // Count the number of integers divisible by both 3 and 5
        for (int i = 15; i < n; i += 15) {
            count--;
        }
 
        // Count the number of integers divisible by 2, 3, and 5
        for (int i = 30; i < n; i += 30) {
            count++;
        }
 
        // Print the final count
        System.out.println("The number of positive integers less than " + n + " that are divisible by either 2, 3, or 5 is " + count + ".");
    }
}

                    

Output
The number of positive integers less than 100 that are divisible by either 2, 3, or 5 is 73.

Time complexity :- O(2^n) or O(n^k)

Auxiliary Space :- O(n log log n)


Article Tags :