What is the difference between permutations and combinations?
Permutations count arrangements where order matters; combinations count selections where order does not. Choosing r from n gives nPr = n!/(n-r)! ordered ways and nCr = n!/(r!(n-r)!) unordered ways.
Explain it simply
Picking a team captain, vice-captain and wicketkeeper from 10 players is a permutation: giving Rohit the captaincy and Virat the vice-captaincy is different from the other way round. Picking 3 players for a committee is a combination: the same three people form the same committee in any order.
Every group of 3 can be arranged in 3! = 6 orders, so permutations = combinations x 6.
Example
- Ordered, 3 roles from 10 people: 10 x 9 x 8 = 720
- Unordered, a 3-person committee: 720 / 6 = 120
import math
print(math.perm(10, 3)) # 720
print(math.comb(10, 3)) # 120Link to probability: if 3 people are picked at random from the 10 and you want a particular trio, the chance is 1/120.
What to say in the interview
"If order matters, such as rankings, passwords or seating, I use permutations, nPr. If it does not, such as choosing a committee or a hand of cards, I use combinations, nCr. A quick check is whether swapping two chosen items gives a different result."
Common mistakes
- Using combinations for passwords or PINs. Order matters there, and repeats may be allowed, which changes the formula again.