
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Permutations and Combinations in C++: Concept & Examples
Permutations and Combinations refer to the arrangements of objects in mathematics.
Permutation ? In permutation, the order matters. Hence, the arrangement of the objects in a particular order is called a permutation.
Permutations are of two types ?
Permutation with repetition
Suppose we have to make a three-digit code. Some possible numbers are 123, 897, 557, 333, 000, and 001. So how many numbers can we make like this?
Let us look at it this way?
In the once place, we have ten options ? 0-9
Similarly, at the tenth and the hundredth place also, we have ten options. 0-9.
Hence, the total options we have are 10*10*10 = 1000.
So, we can make 1000 different permutations with the repetitions of digits.
Generalizing? If we have n choices and r places to fill, we can make n*n*n?(r times) permutations.
Hence the formula for the number of permutations with repetition is nr.
Permutation without repetition
Now, we have to make a three-digit code without the repetition of digits.
For example, 123, 019, 345, 876, etc.
In the once place, we have ten choices? 0-9.
At the tenth place, we have nine choices ? 0-9 (excluding the once place digit)
At the hundredth place, we have eight choices.
Hence, the total number of choices we have are 8*9*10.
Factorial
A factorial of a number refers to the product of all the positive integers less than and equal to the number.
It is denoted by an exclamation mark after the number.
For example?
1! = 1. 2! = 2*1 = 2. 3! = 3*2*1 = 6. 4! = 4*3*2*1 = 24. OR 4! = 4*3! = 24 Hence n! = n*(n-1)!
Now, if we want to find out 10*9*8, we can write it as ?
$$\frac{10!}{\lgroup 10-3\rgroup!}=\frac{10!}{7!}$$
$$=\frac{10*9*8*7!}{7!}$$
$$=10*9*8$$
Hence, we can represent the number permutations without repetition as ?
$$\frac{n!}{\lgroup n-r\rgroup!}$$
Where we have to choose out of total n things r times.
Notation
$$P\lgroup n,r\rgroup = nPr = nPr = \frac{n!}{\lgroup n-r\rgroup!}$$
Points to Remember
nP0 = 1
nP1 = n
nPn-1 = n!
Combination
In combination, we select the items and the order doesn't matter. We can understand a combination as taking out k objects from total n objects without repetition.
Example
Consider that we want to place ?123' in some order. The possibilities are 123, 132, 321, 312, 213, and 231. That is, there are 3! = 3*2*1 = 6 possibilities.
Now, if the order doesn't matter, there is only one possibility, that is: choosing all three of '123' and selecting them.
Formula
Hence, we adjust our permutations formula to reduce it by how many ways the objects could be in order (because we aren't interested in their order anymore) ?
$$\frac{n!}{r!\lgroup n-r\rgroup!}$$
Where n is the number of things to choose from, and r is the number of things we choose. The order doesn't matter, and no repetition is allowed.
Notation
$$C\lgroup n,r\rgroup = nCr = nCr =\frac{n!}{r!(n-r)!}$$
Sample Problems
Q1. In how many of the distinct permutations of the letters in MISSISSIPPI do the four ?I'(s) not come together?
Solution
Given word: - MISSISSIPPI M - 1 I - 4 S - 4 P - 2 Number of permutations = 11!/(4! 4! 2!) = (11 Ã 10 Ã 9 Ã 8 Ã 7 Ã 6 Ã 5 Ã 4!)/ (4! Ã 24 Ã 2) = 34650 Now, we will remove the permutations in which the four ?I'(s) come together. We take that 4 I'(s) come together, and they are treated as 1 letter, ? Total number of letters=11 - 4 + 1 = 8 ? Number of permutations = 8!/(4! 2!) = (8 Ã 7 Ã 6 Ã 5 Ã 4!)/ (4! Ã 2) = 840 Therefore, the total number of permutations where four 'I'(s) don't come together = 34650 - 840 = 33810
Q2. A group consists of 4 girls and 7 boys. In how many ways can a team of 5 members be selected if the team has ?
no girls
at least one boy and one girl
at least three girls
Solution
Given, Number of girls = 7 Number of boys = 7
No girls
Total number of ways the team can have no girls = 4C0 Ã 7C5 = 1 Ã 21 = 21
at least one boy and one girl
1 boy and 4 girls = 7C1 Ã 4C4 = 7 Ã 1 = 7 2 boys and 3 girls = 7C2 Ã 4C3 = 21 Ã 4 = 84 3 boys and 2 girls = 7C3 Ã 4C2 = 35 Ã 6 = 210 4 boys and 1 girl = 7C4 Ã 4C1 = 35 Ã 4 = 140 Total number of ways the team can have at least one boy and one girl = 7 + 84 + 210 + 140 = 441
At least three girls
Total number of ways the team can have at least three girls = 4C3 Ã 7C2 + 4C4 Ã 7C1 = 4 Ã 21 + 7 = 84 + 7 = 91
To find the permutations and combinations, we need to find the factorial of the number. Hence the function for finding the factorial of a number is given below ?
//Function to find the factorial of a number. int factorial(int n) { if (n == 0 || n == 1){ return 1; } //Recursive call return n * factorial(n - 1); }
Example
C++ Program for finding the number of Permutations without repetition
Now we can utilize the above function and the formulae to compute the permutation and combination ?
#include <bits/stdc++.h> using namespace std; //Function to find the factorial of a number. int factorial(int n) { if (n == 0 || n == 1){ return 1; } //Recursive call return n * factorial(n - 1); } //Driver Code int main() { int n = 4, r = 2; //Calculating the combinations using the formula int combinations = factorial(n) / (factorial(r) * factorial(n-r)); cout << "The number of Combinations is : " << combinations<<endl; //Calculating the permutations using the formula int permutations = factorial(n) / factorial(n-r); cout << "The number of Permutations is : " << permutations<<endl; return 0; }
Output
For input n=4, r=2, the above C++ program will produce the following output ?
The number of Combinations is : 6 The number of Permutations is : 12
Conclusion
In this article, we understood the concept of permutations and combinations. We saw the formulae, notations, and a few examples and sample problems. At last, we also saw the program in C++.