Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Number System and Arithmetic
  • Algebra
  • Set Theory
  • Probability
  • Statistics
  • Geometry
  • Calculus
  • Logarithms
  • Mensuration
  • Matrices
  • Trigonometry
  • Mathematics
Open In App
Next Article:
Inclusion-Exclusion and its various Applications
Next article icon

Inclusion-Exclusion and its various Applications

Last Updated : 18 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 
 

  • For 2 finite sets A_1         and A_2         , which are subsets of Universal set, then (A_1-A_2), (A_2-A_1)         and (A_1\bigcap A_2)         are disjoint sets. 
     


  • Hence it can be said that, 
    \left|(A_1-A_2)\bigcup(A_2-A_1)\bigcup(A_1\bigcap A_2)\right| = \left|A_1\right| - \left|A_1\bigcap A_2\right| + \left|A_2\right| - \left|A_1\bigcap A_2\right| + \left|A_1\bigcap A_2\right|         
    \left|A_1 \bigcup A_2\right| = \left|A_1\right| + \left|A_2\right| -\left|A_1 \bigcap A_2\right|         .
  • Similarly for 3 finite sets A_1         , A_2         and A_3         , 
    \left|A_1 \bigcup A_2 \bigcup A_3\right| = \left|A_1\right| + \left|A_2\right| + \left|A_3\right| - \left|A_1 \bigcap A_2\right| - \left|A_2 \bigcap A_3\right| - \left|A_1 \bigcap A_3\right|+ \left|A_1 \bigcap A_2 \bigcap A_3\right|


 

Principle :


Inclusion-Exclusion principle says that for any number of finite sets A_1, A_2, A_3... A_i         , 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 .. + (-1)^{i+1}         Sum of all the i-set intersections. 
In general it can be said that, 
 

\left|A_1 \bigcup A_2 \bigcup A_3 .. \bigcup A_i\right| = \sum\limits_{1 \leq k \leq i} \left|A_k\right| + (-1)\sum\limits_{1 \leq k_1 \textless k_2 \leq i} \left|A_{k_1} \bigcap A_{k_2}\right| + (-1)^2\sum\limits_{1 \leq k_1 \textless k_2 \textless k_3 \leq i} \left|A_{k_1} \bigcap A_{k_2} \bigcap A_{k_3}\right| .. + (-1)^{i+1}\sum\limits_{1 \leq k_1 \textless k_2 \textless k_3 \textless .. k_i\leq i} \left|A_{k_1} \bigcap A_{k_2} \bigcap A_{k_3} ..\bigcap A_{k_i}\right| 


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 \left|A \bigcup B \bigcup C\right|         . 
 


Solution : 
The values of the corresponding regions, as can be noted from the diagram are - 
\left|A\right| = 2, \left|B\right| = 2, \left|C\right| = 2, \left|A \bigcap B\right| = 3, \left|B \bigcap C\right| = 3,         
\left|A \bigcap C\right| = 3, \left|A \bigcap B \bigcap C\right| = 4         
By applying Inclusion-Exclusion principle, 
\left|A_1 \bigcup A_2 \bigcup A_3\right| = \left|A_1\right| + \left|A_2\right| + \left|A_3\right| - \left|A_1 \bigcap A_2\right| - \left|A_2 \bigcap A_3\right| - \left|A_1 \bigcap A_3\right|+ \left|A_1 \bigcap A_2 \bigcap A_3\right|         
\left|A_1 \bigcup A_2 \bigcup A_3\right| = 2 + 2 + 2 - 3 - 3 - 3 + 4 = 1         
 

Applications :


 

  • Derangements 
    To determine the number of derangements( or permutations) of n objects such that no object is in its original position (like Hat-check problem). 
    As an example we can consider the derangements of the number in the following cases: 
    For i = 1, the total number of derangements is 0. 
    For i = 2, the total number of derangements is 1. This is 2 1         . 
    For i = 3, the total number of derangements is 2. These are 2 3 1         and 3 1 2.

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:

C++
#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; } 
Python3
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}.") 
Java
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)


Next Article
Inclusion-Exclusion and its various Applications

A

Anand_Shekhar
Improve
Article Tags :
  • Mathematics

Similar Reads

    Propositional Logic
    Logic is the basis of all mathematical reasoning and all automated reasoning. The rules of logic specify the meaning of mathematical statements. These rules help us understand and reason with statements such as -\exists~x~such~that~x~\neq~a^2~+~b^2,~where~\:x,~a,~b\in~ZWhich in Simple English means
    10 min read
    Propositions Laws and Algebra
    Propositional logic is the foundation of logical reasoning, playing a vital role in understanding mathematical proofs and algorithms. It is especially important for college students preparing for competitive exams like GATE.This article explores: Fundamental laws and concepts in the algebra of propo
    7 min read
    Propositional Equivalences
    Propositional equivalences are fundamental concepts in logic that allow us to simplify and manipulate logical statements. Understanding these equivalences is crucial in computer science, engineering, and mathematics, as they are used to design circuits, optimize algorithms, and prove theorems. This
    7 min read
    Predicates and Quantifiers
    Predicates and Quantifiers are fundamental concepts in mathematical logic, essential for expressing statements and reasoning about the properties of objects within a domain. These concepts are widely used in computer science, engineering, and mathematics to formulate precise and logical statements.
    6 min read
    Predicates and Quantifiers - Set 2
    Before diving into the article, you need to know about predicates, quantifiers, and propositional logic rules.Logical Equivalences involving QuantifiersTwo logical statements involving predicates and quantifiers are considered equivalent if and only if they have the same truth value no matter which
    5 min read
    Mathematics | Some Theorems on Nested Quantifiers
    Quantifiers are expressions that indicate the scope of the term to which they are attached, they are predicates. A predicate is a property the subject of the statement can have. For example, in the statement "the sum of x and y is greater than 5", the predicate 'Q' is- sum is greater than 5, and the
    6 min read
    Rules of Inference
    Rules of Inference: Rules of inference are logical tools used to derive conclusions from premises. They form the foundation of logical reasoning, allowing us to build arguments, prove theorems, and solve problems in mathematics, computer science, and philosophy. Understanding these rules is crucial
    9 min read
    PDNF and PCNF in Discrete Mathematics
    PDNF (Principal Disjunctive Normal Form)It stands for Principal Disjunctive Normal Form. It refers to the Sum of Products, i.e., SOP. For eg. : If P, Q, and R are the variables then (P. Q'. R) + (P' . Q . R) + (P . Q . R') is an example of an expression in PDNF. Here '+' i.e. sum is the main operato
    4 min read
    Set Theory
    Set theory is a branch of mathematics that deals with collections of objects, called sets. A set is simply a collection of distinct elements, such as numbers, letters, or even everyday objects, that share a common property or rule.Example of SetsSome examples of sets include:A set of fruits: {apple,
    3 min read
    Set Operations
    A set is simply a collection of distinct objects. These objects can be numbers, letters, or even people—anything! We denote a set using curly brackets.For example: A = {1, 2, 3}Set Operations can be defined as the operations performed on two or more sets to obtain a single set containing a combinati
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences