Imagine you're organizing a team selection process where you need to pick exactly k people from a group of n candidates numbered from 1 to n. Your task is to generate all possible team combinations that can be formed.
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. The order of combinations in your result doesn't matter, but each combination should contain unique numbers.
Example: If n=4 and k=2, you could form teams like [1,2], [1,3], [1,4], [2,3], [2,4], [3,4] - that's 6 different combinations!
Input & Output
Visualization
Time & Space Complexity
We generate exactly C(n,k) combinations, and each takes O(k) time to construct. The pruning optimization doesn't change worst-case but improves average performance.
We store C(n,k) combinations each of size k, plus O(k) space for recursion stack depth
Constraints
- 1 โค k โค n โค 20
- k cannot exceed n (you can't choose more items than available)
- Output size grows exponentially - C(n,k) can be up to C(20,10) = 184,756