Combinations vs permutations
easy 15 minPermutations count arrangements where order matters (AB is not BA); combinations count selections where it does not ({A, B} is {B, A}).
Video lesson · Chapter 1 of 6
Who is on the microphone?
Combinations vs. permutations
1:25Reel
Read caption
Moss casts two of four frogs, comparing six unordered duets with twelve assignments to distinct roles. Swap the singers. Does the outcome change?
How it works
Step through the idea, or press Play.
Ask: does order matter?. If swapping two chosen items gives a different outcome, it is a permutation; if not, a combination
Understand it
Start here
Order matters, or it does not
Both ideas answer "in how many ways can I pick things from ?". The only difference is whether the order of the picks counts. A permutation is an arrangement: ABC and CBA are different. A combination is a selection: and are the same group.
Permutation, written P(n, r) or nPr
- Order matters:
AB≠BA. - Passwords, PINs, rankings, podium places, seating plans.
- "Pick and place in order."
Combination, written C(n, r), nCr or
- Order does not matter: .
- Teams, committees, lottery tickets, pizza toppings.
- "Just pick, do not arrange."
Password: 123
123, 321 and 213 open different locks. Wrong order means wrong password, so it is a permutation.
Committee of 3
Alice, Bob and Cara form one committee whatever order you name them in, so it is a combination.
The decision
Which formula do I use?
Order matters? Repeats allowed?
Quick reference
| Scenario | Type | Count |
|---|---|---|
| Arrange books on a shelf | Permutation, | |
| Podium: 1st, 2nd, 3rd of runners | Permutation | |
| Pick 3 of 10 people, no roles | Combination | |
| All subsets of items | Sum of every | |
| 8-character password from 62 symbols | Sequence with repeats | trillion |
Arrangements
Counting permutations
Fill positions one by one. The first position has choices, the second (one item is used up), and so on until the -th position has . Multiply the choices together.
The permutation formula
Arrange 2 of {A, B, C, D}
Worked example
Selections
Counting combinations
The combination formula
Worked example
from math import comb, perm, factorialprint(perm(10, 3), comb(10, 3)) # 720 120print(perm(4, 2), comb(4, 2)) # 12 6print(factorial(10) // factorial(7)) # 720, by handprint(factorial(10) // (factorial(3) * factorial(7))) # 120, by handThe key insight
Why we divide by r!
Pick 2 from {A, B, C}
| Permutations, | Same group as | Combinations, |
|---|---|---|
AB, BA | 1 | |
AC, CA | 1 | |
BC, CB | 1 |
Six arrangements collapse into three groups
Permutations = combinations × orderings
Without factorials
Pascal's triangle
Write in rows: row holds to . Each row starts and ends with 1, and every inner number is the sum of the two numbers above it. You can compute any with additions only, and the intermediate numbers never get bigger than the answer.
Pascal's rule
| Row | Values | Row sum |
|---|---|---|
| 0 | 1 | 1 = |
| 1 | 1 1 | 2 = |
| 2 | 1 2 1 | 4 = |
| 3 | 1 3 3 1 | 8 = |
| 4 | 1 4 6 4 1 | 16 = |
| 5 | 1 5 10 10 5 1 | 32 = |
| 6 | 1 6 15 20 15 6 1 | 64 = |
Every row sums to a power of two
Shortcuts
Identities worth knowing
| Identity | Formula | Why |
|---|---|---|
| Edges | One way to choose nothing, one way to choose everything | |
| Symmetry | Choosing who is in is the same as choosing who is out | |
| Pairs | Handshakes among people; pairs in a nested loop | |
| All orders | Arrange every item | |
| Repeated items | Arrangements of a word with repeated letters |
def n_choose_r(n, r): if r < 0 or r > n: return 0 r = min(r, n - r) # symmetry: fewer steps result = 1 for k in range(1, r + 1): result = result * (n - r + k) // k # always an exact integer return resultprint(n_choose_r(4, 2)) # 6print(n_choose_r(10, 3)) # 120print(n_choose_r(100, 98)) # 4950, computed as C(100, 2)In code
Generating them with backtracking
One backtracking call
def permute(nums): result, path = [], [] used = [False] * len(nums) def backtrack(): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue used[i] = True path.append(nums[i]) backtrack() path.pop() used[i] = False backtrack() return resultdef combine(n, k): result, path = [], [] def backtrack(start): if len(path) == k: result.append(path[:]) return for i in range(start, n + 1): path.append(i) backtrack(i + 1) # the key line path.pop() backtrack(1) return resultprint(permute([1, 2, 3])) # 6 orderingsprint(combine(4, 2)) # [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]Permutations: used[] array
- Loop over every index each time.
- Skip indexes already in the path.
- Undo both
path.pop()andused[i] = False.
Combinations: start index
- Loop from
startonly. - Recurse with
i + 1so the next pick comes strictly later. - No used[] needed: earlier items are never offered again.
Edge case
When the input has duplicates
With repeated values, plain backtracking prints the same arrangement more than once: the two 1s in [1, 1, 2] are interchangeable. The count of distinct arrangements divides out the orderings of each repeated value.
Arrangements of a multiset
aabb has distinct arrangements, not 24.def permute_unique(nums): nums = sorted(nums) result, path = [], [] used = [False] * len(nums) def backtrack(): if len(path) == len(nums): result.append(path[:]) return for i in range(len(nums)): if used[i]: continue if i > 0 and nums[i] == nums[i - 1] and not used[i - 1]: continue # same value, same position: already tried used[i] = True path.append(nums[i]) backtrack() path.pop() used[i] = False backtrack() return resultprint(permute_unique([1, 1, 2])) # [[1, 1, 2], [1, 2, 1], [2, 1, 1]]Cost
How expensive is generating them?
| Task | Results | Time | Extra space |
|---|---|---|---|
| All permutations of | recursion | ||
| Permutations of from | |||
| Combinations of from | |||
| All subsets of | recursion |
Factorial beats exponential
Why engineers care
Where the counts show up
Complexity analysis
Brute force over orderings is , over subsets . Knowing the count tells you when to reach for DP.
Query optimisers
Joining tables can happen in orders: 120 for 5 tables, 3,628,800 for 10. Optimisers must prune.
Security
A password of length over symbols has options. 8 characters from 62 gives about 218 trillion.
Feature selection
Choosing 10 of 100 features: trillion subsets, so exhaustive search is out.
Interview staples
Permutations, Permutations II, Combinations, Subsets, Combination Sum and Next Permutation all build on this page.
Probability
Odds are favourable outcomes over total outcomes, and both are usually a or a .
Recap
Key takeaways
Key points
- Ask "does order matter?" first: yes is a permutation, no is a combination.
- : multiply the choices for each position,
- : divide out the orderings of each group.
- Pascal's rule builds every value with additions; each row sums to .
- Backtracking: permutations use a used[] array, combinations recurse with
i + 1. - Repeats allowed change the formula (), and duplicate inputs need sort-and-skip.
- Listing all permutations is ; count with a formula whenever you only need the number.