Foundation · Module 2 · Lesson 4
Combinatorics
Permutations, combinations, and counting choices.
15–25 min readLesson 10 of 123
Lesson 10 of 123
1d streakTheory lesson · Step-by-step
Understand Combinatorics
Permutations, combinations, and counting choices.
Worked approach
Trace the reasoning step by step
- 1Write the relevant identity or bit property for Combinatorics.
- 2Normalize signs, ranges, and overflow-sensitive values before calculating.
- 3Apply one valid transformation and verify the identity still holds.
- 4Stop at the simplest base form, then translate the result back to the requested output.
- 5Test zero, one, negative values, repeated factors, and numeric limits.
Concrete example
Choose 3 members from 5 when order does not matter.
- 1Count ordered selections: 5 × 4 × 3.
- 2Each group is counted 3! times.
C(5,3) = 60 / 6 = 10.
What must remain true (Loop Invariant)
Every transformation used by Combinatorics preserves the mathematical quantity or relation being computed.
Reference pseudocode
1combinatorics(input):
2 normalize input and define the mathematical state
3 while the state can be reduced:
4 apply one identity-preserving transformation
5 convert the reduced state into the requested result
6 return result
Verify your trace
- • Write the state after every step.
- • Confirm the invariant still holds.
- • Count visited states to derive complexity.
- • Repeat with an edge-case input.
Cost Model
Complexity & Trade-offs
OperationTimeSpaceWhy
Compute one binomial coefficient
O(k)
O(1)
The bound follows from the number of states visited and the work performed per transition.When to use it
- Counting, divisibility, modular cycles, masks, and overflow-safe arithmetic.
- Replacing simulation with a mathematical identity.
- Encoding compact state for search and dynamic programming.
Common mistakes
- Using an identity without checking its domain or overflow behavior.
- Forgetting how the language handles negative remainder and signed shifts.
- Confusing value magnitude with the number of input values.
Practice prompts
- Reproduce the worked Combinatorics example without looking at the trace.
- Change one input condition and explain whether the Combinatorics invariant still holds.
- Implement the approach, derive its cost, and compare it with a straightforward baseline.
Mastery check
Can you derive it instead of memorizing it?
State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.