Welcome to the world of competitive programming, where we’ll explore the ABCs of ** Combinatorics** – a fancy term for counting and arranging things. Think of it like solving puzzles with numbers and patterns. In this article, we’re diving into the

**that every competitive programmer needs to know. We’ll learn how to count, arrange, and select things in clever ways. These skills are like secret weapons for solving problems quickly and efficiently. So, get ready to boost your problem-solving game with the magic of Combinatorics!**

**Basics of Combinatorics**Table of Content

__What is Combinatorics?__

is a branch of mathematics that focuses onCombinatoricsdiscrete objects and structures. It deals with the study of different ways to select, arrange, and combine elements from finite sets or sequences, often in the context of discrete and finite structures. Combinatorics encompasses a wide range of topic.counting, arranging, and analyzing

__Basic Rules of Combinatorics:__

__1. The Sum Rule:__

If we have ** A** number of ways of doing Task1 and

**number of ways of doing Task2 then the total number of ways to choose one of the Task is equals to**

**B**

**A+B.**So generally, if there are ** N** tasks and i’th task can be done in a[i] ways then there are

**ways to do one of the tasks.**

**a1+ a2+ a3+… an****Example:**

Imagine you have 3 different Hats, 2 different shirts and 4 different pants. If you want to donate any one of the item what are the total number of ways to do say?

3 + 2 + 4 = 9Answer:

__2. The Product Rule:__

If we have A number of ways of doing Task1 and B number of ways of doing Task2 then the total number of ways of doing both the tasks is equal to A*B.

So generally, if there are N tasks and i’th task can be done in a[i] ways then there are a1* a2* a3*… an ways to do all the task.

** Example: **Imagine you have 3 different Hats, 2 different shirts and 4 different pants. You want to get dressed and for that you have to wear 1 hat, 1 shirt and 1 pant. In how many ways you can do so?

** Solution: **3 * 2 * 4 = 24

__Fundamental Concepts in Combinatorics__

Here, we’r going to introduce you to several fundamental concepts in combinatorics that is very useful:

__1. ____Factorial:__

The main use of factorial is counting the number of permutations (number of ways of arranging some objects). It can be used to answer following types of questions:

A class has just 3 seats vacant. Three people P, A, and R arrive at the same time. In how many ways can P, A, and R be arranged on those 3 vacant seats?**Question 1.**Find the number of ways of arranging 5 people if 2 of them always sit together?**Question 2.**Find all the three-letter words beginning and ending with a vowel. Given that repetition of alphabets is not allowed.**Question 3.Â**- click here to view how to use factorial in order to solve.

__2. ____Permutations:__

In permutation, we primarily deal with four kinds of problems.

**A. ****Permutation with repetition:**

**A.**

**Permutation with repetition:**** Question: **How many 3-digit numbers greater than 500 can be formed using 3, 4, 5, and 7?

: Since a three-digit number, greater than 500 will have either 5 or 7 at its hundredth place, we haveÂ 2 choicesÂ for this place. There is no restriction on repetition of the digits, hence for the remaining 2 digits we haveÂ 4Â choicesÂ each. So the total permutations are: 2 Ã— 4 Ã— 4 = 32Solution

**B. ****Permutation without repetition:**

**B.**

**Permutation without repetition:**** Question:** How many 3-digit numbers divisible by 3 can be formed using digits 2, 4, 6, and 8 without repetition?

: For a number to be divisible by 3, the sum of it digits mustÂ be divisible by 3SolutionFrom the given set, various arrangements like 444 can beÂ formed but since repetition isnâ€™t allowed we wonâ€™t beÂ considering them.

We are left with just 2 cases i.e. 2, 4, 6 and 4, 6, 8

The number of arrangements are 3! in each case

Hence the total number of permutations are: 3! + 3! = 12

**C. ****r – permutation without repetition:**

**C.**

**r – permutation without repetition:**This is when we arrange just ** r** objects out of

**objects without repetition**

**n**

**.**** Question: **An ice-cream shop has 10 flavors of ice cream. Find the number of ways of preparing an ice cream cone with any 3 different flavors?

Let us consider n = 10 (total number of flavors) and r = 3 (number of different flavors needed)Solution:For first flavor we have 10 choices

For second flavor we have 10 – 1 choices

For third flavor we have 10 – 2 choices and this is same as (n – r + 1)The numbers of arrangement would be: 10 Ã— (10 – 1) Ã— (10 – 3 + 1) = 720

From this we can generalize that, the number of ways of arranging r objects out of n different objects is:

n Ã— (n – 1) Â . . . (n – r + 1) =Â nPr

#### D. __r- permutation with repetition:__

This can be thought of as the distribution of ** n** objects into

**boxes where the repetition of objects is allowed, and any box can hold any number of objects.**

**r**** Question:** A police officer visits the crime scene 3 times a week for investigation. Find the number of ways to schedule his visit if there is no restriction on the number of visits per day?

The number of ways to schedule first visit is 7 (any of the 7 days)Solution:

The number of ways to schedule second visit is 7 (any of the 7 days)

The number of ways to schedule third visit is 7 (any of the 7 days)Hence, the number of ways to schedule first and second andÂ third visit is 7 Ã— 7 Ã— 7 = 73Â = 343

__3. ____Combinations:__

A combination of a set of** distinct** objects is just a count of the number of ways a specific number of elements can be

**from a set of a certain size. The**

**selected****of elements**

**order****in a combination.Â**

**does not matter**An unordered selection ofÂ r elements from a set is called anÂ

**. It is represented asÂ C(n,r)**

**r-combination**Since a combination is just a permutation without order, the number ofÂ r-combination can be expressed in terms ofÂ r-permutation.Â

TheÂ r-permutation can be obtained by first obtaining theÂ r-combination and then ordering the elements in eachÂ r-combination, which can be done inÂ P(r, r) ways.Â

which gives us:

** Question**: From a class of 30 students, 4 are to be chosen for the competition. In how many ways can they be chosen?

: Total students = n = 30Solution

Number of students to be chosen = r = 4

Hence, Total number of ways 4 students out of 30 can be chosen is,

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â^{30}C_{4}Â = (30 Ã— 29 Ã— 28)/ (4 Ã— 3 Ã— 2 Ã— 1)

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = 24360/ 24

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = 1015 ways

** Question: **Given two integers n and m, your task is to calculate the number of arrays such that below 4 conditions are satisfied:

- Size of array must be n
- All array element must be from 1 to m;
- For each array, there is exactly one pair of duplicate elements
- For each array there exists an i such that array is strictly increasing before i’th index and strictly decreasing after the i’th element.

** Example**: n=3, m=4

**: 6**

**Output****: [1,2,1], [1,3,1], [1,4,1], [2,3,2], [2,4,2], [3,4,3] are the arrays that satisfy the conditions.**

**Explanation**

Solution using combinatorics:Using condition 1 and 3, there will be exactly n-1 distinct elements in the array which is equal toNow we should have exactly one element that appears twice and we can choose from n-1 elements, but we cannot choose the maximum element because it will contradict our 4th condition. So, multiply our answer by (n-2)

finally, some elements will appear earlier than the maximum in our array, and some â€” later. The duplicated element will appear on both sides, but all other elements should appear either to the left or to the right, so there are Â Â ways to choose their positions.

Thus, the final answer is

__4. ____Binomial Coefficient__

AÂ binomial coefficientÂ ** C(n, k)** can be defined as the coefficient of

**in the expansion of**

**x^k****. A binomial coefficient**

**(1 + x)^n****also gives the number of ways, disregarding order, that**

**C(n, k)****objects can be chosen from among**

**k****objects more formally, the number of k-element subsets (or k-combinations) of a n-element set.**

**n**Formally,Â letÂ

**andÂ**

**x****be variables and**

**y****be a non-negative integer. ThenÂ**

**Â n**__5. ____Inclusion Exclusion Principle__

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Â

**Â such that first element is greater thanÂ**

**[0,9]****Â and last element is smaller thanÂ**

**1****.**

**8**

We can revert the problem using the principle such that instead of calculating the original problem we can do the following:Solution using Inclusion Exclusion:

- 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Â

Then by Inclusion Exclusion Principle:

=Â 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 â€˜

**exists consisting of only charactersÂ**

**Nâ€™Â****and**

**â€˜aâ€™, â€˜bâ€™Â****such that each character occurs at least once.**

**Â â€˜câ€™Â**

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 subsequences possibleÂ (3^n)Solution using Inclusion Exclusion:LetÂ A = number of subsequences where â€˜aâ€™ does not occur

LetÂ B = number of subsequences where â€˜bâ€™ does not occur

LetÂ C = number of subsequences where â€˜câ€™ does not occur

Then usingÂ Inclusion-Exclusion Principle:

which can be simplified to,

Subtract this count from total number of subsequence possible i.e 3^n to get the answer.

__Pigeon Hole Principle:__

If you have more pigeons than pigeonholes into which you want to distribute those pigeons, at least one pigeonhole must contain more than one pigeon.

In other words, if you have ** n** objects and

**containers, where**

**m****, then at least one of the**

**n > m****containers must contain more than one object. This principle is named after the analogy of pigeons (objects) being placed in pigeonholes (containers).**

**m**__Practice Problems on Combinatorics:__

Problem | Practice |
---|---|

Perfect Sum Problem | |

Combination Sum | |

Permutations of a given string | |

Subsets with XOR value | |

nCr mod M | Part 1 | |

Combination Sum II | |

Combination Sum III | |

nPr | |

Calculate the coefficient | |

Maximum Sum Combination |

**Related articles: **

- Permutations and Combinations
- Permutation vs Combination – Definition, Differences and Examples
- Pigeonhole Principle for CP | Identification, Approach and Problems
- Mathematics | Combinatorics Basics
- Mathematics | PnC and Binomial Coefficients