Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get an array candidates of distinct positive integers and a positive integer target. Return every combination of candidates whose sum is exactly target. You may use the same number as many times as you like. Two combinations are the same if they contain the same numbers the same number of times, so [2, 2, 3] and [3, 2, 2] count once. Any order of the output is fine.
Limits that matter: up to 30 candidates, each between 2 and 40, and target is at most 40. The problem promises fewer than 150 answers.
Example: candidates = [2, 3, 6, 7], target = 7 → [[2, 2, 3], [7]]. Using 2 + 2 + 3 = 7 and 7 = 7.
Start with brute force
Generate every sequence of candidates, adding numbers until the sum reaches or passes the target, and keep the ones that hit it exactly. That finds every answer, but it finds each one many times: [2, 2, 3], [2, 3, 2] and [3, 2, 2] are all generated. You'd need to sort each result and put it in a set to remove the copies.
That wastes most of the work. A combination of length k can show up as up to k! different sequences, so the tree is far bigger than the answer. The fix is to make duplicates impossible instead of cleaning them up afterwards.
Building the intuition
Start from Subsets. There, after choosing nums[i], you only looked at elements to the right of i. That rule is what made [2, 1] impossible once you had [1, 2]. Every subset was built in one order only: increasing index.
Use the same rule here: every combination is built with indices that never go down. [2, 2, 3] can only be built as 2, then 2, then 3. [3, 2, 2] would need a step from index 1 back to index 0, which is not allowed. Duplicates are gone.
Now the reuse. In Subsets the next call was backtrack(i + 1), which means "strictly to the right". Here you may pick the same number again, so the next call is backtrack(i): "this index or to the right". That one-character change is the whole difference.
Last, the stopping rule. Carry remaining, the part of the target still to fill. When it hits 0, save the path. And because every candidate is at least 2, remaining shrinks every step, so the recursion always ends.
Here is part of the tree for [2, 3, 6, 7], target 7 (each node shows the path and what's left):
[] 7
/ | \ \
[2] 5 [3] 4 [6] 1 [7] 0 ✓
/ \ |
[2,2] 3 [2,3] 2 [3,3] 1
|
[2,2,3] 0 ✓[2,2,2] is missing because 2 > 1 is rejected straight away, which brings us to pruning.
The optimal approach
- Sort
candidates. This lets you stop a loop early. - Write
backtrack(start, remaining):- If
remaining == 0, save a copy ofpathand return. - For each
ifromstartto the end, withc = candidates[i]:- If
c > remaining, break. The array is sorted, so every later candidate is too big as well. - Choose
c, explore withbacktrack(i, remaining − c), un-choosec.
- If
- If
- Call
backtrack(0, target).
The subtle detail is break versus continue. Without sorting you could only continue, which skips one too-big number but still checks the rest. Sorting turns that into a break, which cuts off the whole rest of the loop. On inputs with many large candidates, that removes most of the tree.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort() # sorted, so we can stop a loop the moment a number is too big
result = []
path = []
def backtrack(start: int, remaining: int) -> None:
if remaining == 0:
result.append(path[:]) # copy: path keeps changing after this
return
for i in range(start, len(candidates)):
c = candidates[i]
if c > remaining:
break # every later candidate is even bigger
path.append(c) # choose
backtrack(i, remaining - c) # explore: i, not i + 1, so c can be reused
path.pop() # un-choose
backtrack(0, target)
return resultclass Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end()); // lets a loop stop once a number is too big
vector<vector<int>> result;
vector<int> path;
backtrack(candidates, 0, target, path, result);
return result;
}
private:
void backtrack(const vector<int>& candidates, int start, int remaining,
vector<int>& path, vector<vector<int>>& result) {
if (remaining == 0) {
result.push_back(path); // push_back copies the vector
return;
}
for (int i = start; i < (int)candidates.size(); i++) {
int c = candidates[i];
if (c > remaining) break; // every later candidate is even bigger
path.push_back(c); // choose
backtrack(candidates, i, remaining - c, path, result); // i, not i + 1: c can be reused
path.pop_back(); // un-choose
}
}
};class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); // lets a loop stop once a number is too big
List<List<Integer>> result = new ArrayList<>();
backtrack(candidates, 0, target, new ArrayList<>(), result);
return result;
}
private void backtrack(int[] candidates, int start, int remaining,
List<Integer> path, List<List<Integer>> result) {
if (remaining == 0) {
result.add(new ArrayList<>(path)); // copy: path keeps changing after this
return;
}
for (int i = start; i < candidates.length; i++) {
int c = candidates[i];
if (c > remaining) break; // every later candidate is even bigger
path.add(c); // choose
backtrack(candidates, i, remaining - c, path, result); // i, not i + 1: c can be reused
path.remove(path.size() - 1); // un-choose
}
}
}/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function (candidates, target) {
candidates.sort((a, b) => a - b); // numeric sort; lets a loop stop once a number is too big
const result = [];
const path = [];
const backtrack = (start, remaining) => {
if (remaining === 0) {
result.push([...path]); // copy: path keeps changing after this
return;
}
for (let i = start; i < candidates.length; i++) {
const c = candidates[i];
if (c > remaining) break; // every later candidate is even bigger
path.push(c); // choose
backtrack(i, remaining - c); // i, not i + 1: c can be reused
path.pop(); // un-choose
}
};
backtrack(0, target);
return result;
};Dry run
candidates = [2, 3, 6, 7] (already sorted), target = 7.
| Call | path before | i, c | What happens |
|---|---|---|---|
bt(0, 7) | [] | 0, 2 | choose 2 → bt(0, 5) |
bt(0, 5) | [2] | 0, 2 | choose 2 → bt(0, 3) |
bt(0, 3) | [2, 2] | 0, 2 | choose 2 → bt(0, 1): 2 > 1, break |
bt(0, 3) | [2, 2] | 1, 3 | choose 3 → bt(1, 0): save [2, 2, 3] |
bt(0, 3) | [2, 2] | 2, 6 | 6 > 3, break |
bt(0, 5) | [2] | 1, 3 | choose 3 → bt(1, 2): 3 > 2, break |
bt(0, 5) | [2] | 2, 6 | 6 > 5, break |
bt(0, 7) | [] | 1, 3 | choose 3 → bt(1, 4): tries 3 → bt(1, 1), dead end |
bt(0, 7) | [] | 2, 6 | choose 6 → bt(2, 1): 6 > 1, break |
bt(0, 7) | [] | 3, 7 | choose 7 → bt(3, 0): save [7] |
Result: [[2, 2, 3], [7]].
Complexity
Let t be the target and m the smallest candidate.
- Time: O(n^(t/m)), roughly. A path can be at most
t/mnumbers long, because each pick removes at leastm. At each level we try at mostncandidates. That's a loose upper bound; sorting andbreakcut far more in practice. Copying a saved path adds O(t/m) per answer. - Space: O(t/m) extra for the recursion depth and
path, not counting the output.
Edge cases and traps
- Recursing with
i + 1. Then each number can be used only once, and[2, 2, 3]is lost. - Recursing with
0. Then you get[2, 2, 3],[2, 3, 2]and[3, 2, 2]: duplicates. breakwithout sorting. On unsorted input[7, 2]with target 4,breakat 7 would skip the 2 and miss[2, 2].- JavaScript's default
sort(). It compares as strings, so[10, 9, 2]sorts to[10, 2, 9]. Always pass(a, b) => a - b. - No answer.
candidates = [2],target = 1returns[]. Nothing special is needed.
Say it in the interview
"It's Subsets with a running total. I backtrack with a start index and the amount still remaining. I save the path when the remaining amount hits zero. To allow reuse I recurse with the same index, not the next one. Because I never go back to an earlier index, every combination is built in non-decreasing order only once, so there are no duplicates. I sort first so I can break out of the loop as soon as a candidate is larger than what's left. The depth is at most target over the smallest candidate."
Follow-ups interviewers ask
- "What if each number can be used only once, and the input has duplicates?" That's Combination Sum II. Recurse with
i + 1, sort, and skipcandidates[i]wheni > startand it equalscandidates[i − 1]. - "What if you only need the number of combinations?" Use dynamic programming:
ways[s] += ways[s − c], looping over coins on the outside. That's the counting version of Coin Change, and it runs in O(n · t). - "Why not memoise the backtracking?" You're listing every combination, so the output alone can be exponential. Memoising lists of lists saves little. DP only wins when you count instead of list.
Practise the same idea
This is Day 70 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free