AlgoMaster Logo

Combination Sum III

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We need to pick exactly k distinct numbers from the set {1, 2, 3, 4, 5, 6, 7, 8, 9} such that they add up to n. The order doesn't matter, so [1, 2, 4] and [2, 1, 4] count as the same combination.

What makes this different from other combination-sum problems is the fixed candidate set. We always work with the digits 1 through 9, and each digit can appear at most once, so there are only 2^9 = 512 possible subsets. Even a brute-force approach that checks every subset is fast enough.

Because order does not matter, we build each combination in increasing order. That single rule produces every combination exactly once and never generates a reordering of one we already have.

Key Constraints:

  • 2 <= k <= 9 → We're picking between 2 and 9 numbers, and the candidate pool is exactly 9 digits. When k = 9, there's only one possible combination: all digits 1 through 9.
  • 1 <= n <= 60 → The maximum possible sum using digits 1-9 is 1+2+...+9 = 45. So any n > 45 guarantees an empty result. The constraint says n can be up to 60, which means some inputs are automatically impossible.

Approach 1: Brute Force (Check All Subsets)

Intuition

With only 512 subsets of {1, 2, ..., 9}, we can generate every one of them and keep the subsets that have exactly k elements and sum to n.

To generate all subsets recursively, walk through the numbers 1 to 9 and, for each one, branch twice: include it or skip it. After a decision has been made for all 9 numbers, check whether the resulting subset meets both criteria.

This version does no pruning. It generates all 512 subsets and checks each one at the end, which the constraints make fast enough.

Algorithm

  1. Start with an empty subset and begin at number 1.
  2. For each number from 1 to 9, make two choices: include it in the current subset, or skip it.
  3. After processing all 9 numbers, check if the subset has exactly k elements and sums to n.
  4. If both conditions are met, add a copy of the subset to the result.
  5. Return all valid subsets.

Example Walkthrough

Input: k = 2, n = 7

0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
candidates

The recursion makes an include-or-skip decision for each digit from 1 to 9, producing 512 leaves. Take one path as an example: include 1, skip 2 through 5, include 6, skip 7 through 9. At the leaf, the subset {1, 6} has exactly 2 elements and the remaining target is 7 - 1 - 6 = 0, so it goes into the result. Across all 512 leaves, exactly three subsets pass both checks:

  • {1, 6}: 1 + 6 = 7
  • {2, 5}: 2 + 5 = 7
  • {3, 4}: 3 + 4 = 7

Every other subset fails the size check or the sum check, so the returned result is [[1, 6], [2, 5], [3, 4]].

0
1
0
1
6
1
2
5
2
3
4
result

Code

This approach commits to all 512 subsets before checking anything. The next approach checks constraints while building and cuts off branches that can no longer succeed.

Approach 2: Backtracking with Pruning

Intuition

The brute force evaluates a subset only after all 9 include-or-skip choices are made. Backtracking checks constraints as it goes: it builds combinations one number at a time, in increasing order, and stops extending a partial combination as soon as nothing valid can come from it.

A branch ends in one of three ways:

  1. We have picked k numbers and the running sum equals n. Record the combination.
  2. We have picked k numbers but the sum is not n. Discard the branch.
  3. The next candidate exceeds the remaining target. Candidates are tried in ascending order and all are positive, so every later candidate exceeds it too, and the loop can stop entirely rather than testing them one by one.

Picking numbers in increasing order (after picking 3, the next try is 4 or higher) also guarantees each combination is generated exactly once, never as a reordering of an earlier one.

Algorithm

  1. Start with an empty combination, start = 1, k open slots, and remaining = n.
  2. At each recursive call, first check the base cases: if k == 0 and remaining == 0, add a copy of the current combination to the result and return. If k == 0 or remaining < 0, return without recording.
  3. Loop num from start to 9. If num > remaining, stop the loop: every later candidate is larger and overshoots too.
  4. Otherwise, add num to the combination and recurse with num + 1 as the new start, k - 1 open slots, and remaining - num.
  5. After the recursive call returns, remove num (backtrack) and try the next candidate.
  6. When the top-level loop finishes, the result holds every valid combination.

Example Walkthrough

Input: k = 3, n = 7

The first view tracks which candidates get picked or pruned. The second view shows the combination being built and unbuilt as the search backtracks.

1Start: pick from [1..9], need k=3 numbers summing to 7
0
1
start
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
1/11
1Current combination: empty. 3 slots, target sum 7.
1/10

Code

Backtracking solves the problem with recursion. The search space is small enough that a single loop over bitmasks can replace the recursion entirely.

Approach 3: Bitmask Enumeration

Intuition

Since we choose from exactly 9 numbers, any subset can be represented as a 9-bit integer. Bit 0 records whether the number 1 is included, bit 1 stands for the number 2, and so on up to bit 8 for the number 9. The bitmask 0b000010110 (decimal 22) has bits 1, 2, and 4 set, so it represents the subset {2, 3, 5}.

Instead of building combinations recursively, iterate through all integers from 0 to 511 (2^9 - 1). For each integer, check two things: does it have exactly k bits set (meaning k numbers chosen), and do the corresponding numbers sum to n? If both conditions hold, extract the combination and add it to the result.

There is no recursion to manage and no backtracking state to undo. The approach works because the search space is small enough to enumerate exhaustively; with a larger candidate set, checking 2^N masks would not be practical.

Algorithm

  1. Iterate through all integers mask from 0 to 511 (2^9 - 1).
  2. For each mask, count the number of set bits. If it's not equal to k, skip.
  3. Calculate the sum of numbers corresponding to set bits. Bit i being set means we include number i + 1.
  4. If the sum equals n, extract the combination (list of numbers where bits are set) and add it to the result.
  5. Return all valid combinations.

Example Walkthrough

Input: k = 2, n = 7

Masks are checked in increasing numeric order, so the matches arrive as [3, 4] (mask 12), then [2, 5] (mask 18), then [1, 6] (mask 33).

1Check every mask from 0 to 511. Keep masks with exactly 2 set bits whose digits sum to 7.
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
1/11

Code