Combination Sum III
Combination Sum III is LeetCode 216 (Medium). Find every combination of exactly k numbers that add up to n, where:
- only the digits 1 to 9 may be used, and
- each digit appears at most once in a combination.
Return the combinations in any order, with no combination listed twice. There is no input array: the pool is always the nine digits, so the search is tiny whatever k and n are.
- 2 <= k <= 9
- 1 <= n <= 60
Intuition
Build each combination in increasing order, one digit per slot, so every set of digits is produced exactly once. A combination is finished only when both counts close together: k slots filled and the remaining sum at 0.
The useful cut looks ahead. Suppose need slots are still empty and you are about to try digit d. The cheapest way to fill them is d, d + 1, …, d + need − 1. If even that smallest finish is bigger than the remaining sum, no digit from d upward can work, so stop the loop. Every larger digit only makes the finish bigger.
Fixed size plus fixed sum over a tiny pool, with every answer listed: a backtracking search over increasing digits, as in Combinations (77), with a sum added.
Approach
Before reading on, try k = 3 and n = 7 by hand. After taking 1 and 3, can any third digit work? How could you know that before trying one?
Fill slots with increasing digits
Write dfs(start, remain), where start is the smallest digit still allowed and remain is what the empty slots must add up to. After taking digit d, call dfs(d + 1, remain - d). Each digit is used once, and every combination comes out in sorted order, so none is repeated.
Check both counts when the slots are full
When the path holds k digits, save a copy only if remain is 0, then return either way. Returning here also stops a path from ever growing past k digits.
Stop once the smallest finish is too big
With need empty slots, the smallest sum digit d can start is need · d + need · (need − 1) / 2, the total of d up to d + need − 1. If that is above remain, break out of the loop. This also covers the plain case d > remain, which is that test with one slot left.
Code in Python, C++ and Java
Common pitfalls
Saving as soon as the sum reaches 0
if remain == 0:
res.append(path[:])
returnif len(path) == k:
if remain == 0:
res.append(path[:])
returnShort paths that already add up to n slip in. With the plain d > remain cut, k = 3 and n = 9 gives [9], [1, 8], [2, 7], [3, 6] and [4, 5] saved next to the three real answers.
Edge cases
The smallest four digits add up to 1 + 2 + 3 + 4 = 10, so the very first check fails and [] comes back without any recursion.
The largest pair is 9 + 8 = 17. The look-ahead only cuts sums that are too small, so the search tries the pairs and every one ends with a remainder, giving []. Only C(9, 2) = 36 pairs exist, so this is still instant.
Complexity
Combination Sum III FAQ
How is it different from Combination Sum II?
Combination Sum II takes an input array with repeated values and no size limit, so it needs a skip for equal neighbours. Here the pool is fixed at the digits 1 to 9 with no repeats, but the answer must have exactly k numbers.
Can the combinations be found with a bitmask instead?
Yes. Each of the 512 masks from 0 to 511 picks a subset of the nine digits; keep the masks with exactly k bits set whose digits add up to n. It is short and correct, but it checks every subset, while the search above stops at the first digit that cannot fit.
Why not prune when the largest finish is too small as well?
It is valid: if the need largest unused digits cannot reach remain, the branch is dead. With only nine digits the search is already tiny, so one cut keeps the code short; add the other if the pool were much larger.