Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Combinations

Problem in Plain English

Given n and k, return every unique group of k distinct numbers drawn from 1 through n. Each group is listed once and the answer order does not matter.

Problem Statement

Given two integers n and k, return every possible combination of k distinct numbers chosen from 1 to n.

List each combination in increasing order, since rearrangements of the same numbers count as one combination.

The full answer may come back in any order.

Constraints

  • 1 <= n <= 20
  • 1 <= k <= n

Examples

Example 1

Input
n = 4, k = 2
Output
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]

Choosing 1 produces [1, 2], [1, 3] and [1, 4]; choosing 2 next produces [2, 3] and [2, 4]; choosing 3 produces [3, 4]. These are the six different pairs. Reversing a pair does not create a new group.

Example 2

Input
n = 1, k = 1
Output
[[1]]

The range holds a single number, so the only combination is [1].

Example 3

Input
n = 4, k = 4
Output
[[1,2,3,4]]

All four numbers are required, so skipping even one makes completion impossible. There is exactly one group, although its elements could be rearranged in many ways; rearrangements are not new combinations.

Example 4

Input
n = 4, k = 1
Output
[[1],[2],[3],[4]]

Only one slot needs filling, so every number individually completes a group. No pair belongs in this output.

Intuition

A combination is a group, so [1, 2] and [2, 1] must not become separate answers. Represent each group by its increasing sequence. In Example 1, choosing 1 leaves only 2, 3 and 4 for the second position; after those pairs, starting with 2 leaves only 3 and 4. A start index enforces this order, and the number of empty slots tells us when too few larger numbers remain.

Approaches

Bitmask Filter

Solution Details

Reveal Bitmask Filter: intuition, complexity, and code

Hints

Hint 1
A subset is determined by whether each number is present. How could you represent those choices with bits and retain only size-k subsets?
Hint 2
Increasing choices give each group one representation. After choosing i, what should the next start index be?
Hint 3
If r slots remain, choosing i needs r - 1 larger numbers afterward. Solve that condition for the last allowed i, then copy completed paths before undoing choices.

Edge Cases

  • n = 1, k = 1 returns [[1]]; the builder reaches its base case after one selection.
  • k = 1 returns each number alone: n = 4 gives [[1],[2],[3],[4]].
  • k = n returns only the whole range. The builder loop has one feasible choice at every depth.
  • n = 20, k = 10 returns 184756 lists holding 1847560 numbers; output storage dominates auxiliary recursion space.

Common Mistakes and Interview Tips

  • Looping every slot from 1 instead of from the next free number, which builds permutations like [2, 1] alongside [1, 2].
  • Forgetting to undo the last pick after recursing, so later branches inherit numbers they never chose.
  • Copying the live path reference into the answer instead of a snapshot, leaving every entry mirroring the final empty path.
  • Using n - remaining instead of n - remaining + 1 as the inclusive upper bound drops valid boundary choices such as first choice 3 in Example 1.
  • Treating the two answer orders as different results: the bitmask approach emits [2,3] before [1,4], which is permitted.
  • Interview tip: separate O(k) auxiliary space from O(k * C(n, k)) returned storage, and explain why shortfall pruning still includes every valid sequence.

Key Takeaway

For fixed-size subsets, keep choices in increasing order to avoid duplicate permutations, then use the number of unfilled slots to exclude prefixes that cannot reach an answer. Copy only complete paths and undo each choice before visiting siblings.