Combinations - Problem
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
example_1.py โ Basic Combination
$
Input:
n = 4, k = 2
โบ
Output:
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
๐ก Note:
All possible ways to choose 2 numbers from [1,2,3,4]. We get C(4,2) = 6 combinations total.
example_2.py โ Single Element
$
Input:
n = 1, k = 1
โบ
Output:
[[1]]
๐ก Note:
Only one way to choose 1 number from [1] - the combination [1] itself.
example_3.py โ Larger Set
$
Input:
n = 5, k = 3
โบ
Output:
[[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]]
๐ก Note:
All possible ways to choose 3 numbers from [1,2,3,4,5]. We get C(5,3) = 10 combinations total.
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
Visualization
Tap to expand
Understanding the Visualization
1
Start Empty
Begin with an empty team and list of all available players [1,2,3,4]
2
Fill Positions
For position 1, try player 1, then explore all possibilities for position 2
3
Complete & Backtrack
When team is full (k players), save it. Then backtrack and try next player
4
Systematic Exploration
Continue until all possible team combinations are found
Key Takeaway
๐ฏ Key Insight: Backtracking with ordered selection ensures we generate all unique combinations exactly once, while pruning optimizations eliminate impossible paths early for better performance.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code