Combinations - Problem

Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

You may return the answer in any order.

A combination is a selection of items without regard to the order of selection.

Input & Output

Example 1 — Basic Case
$ Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
💡 Note: All possible combinations of 2 numbers from range [1,4]: choose 2 from {1,2,3,4}
Example 2 — Single Element
$ Input: n = 1, k = 1
Output: [[1]]
💡 Note: Only one way to choose 1 number from range [1,1]: just [1]
Example 3 — Choose All
$ Input: n = 3, k = 3
Output: [[1,2,3]]
💡 Note: Only one way to choose all 3 numbers from range [1,3]: take everything

Constraints

  • 1 ≤ n ≤ 20
  • 1 ≤ k ≤ n

Visualization

Tap to expand
Combinations: Choose k=2 numbers from n=4 numbersAvailable Numbers:1234All Combinations (choose 2):[1,2][1,3][1,4][2,3][2,4][3,4]Total: C(4,2) = 6 combinationsC(n,k) = n! / (k!(n-k)!)C(4,2) = 4! / (2!×2!) = 24/4 = 6Result: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
Understanding the Visualization
1
Input
n=4, k=2 means choose 2 numbers from {1,2,3,4}
2
Process
Generate all possible combinations systematically
3
Output
Return all valid k-sized combinations
Key Takeaway
🎯 Key Insight: Use backtracking to systematically build combinations one element at a time
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen