Home
DSA Patterns
Backtracking
Combination Sum
Combination Sum (LeetCode 39)
Combination Sum (LeetCode 39): find every combination that sums to a target, reusing numbers freely, and prune with a sorted break.
Examples
2 + 2 + 3 = 7 uses 2 twice, which is allowed; 7 on its own is the other.
Three combinations; no order within or between them matters.
The only candidate is already bigger than the target.
Problem Understanding
Given an array of distinct integers candidates and a target, return a list of all unique combinations where the chosen numbers sum to target.
Key Rule: The same number may be chosen from candidates an unlimited number of times.
Algorithm Strategy
Sort the candidates first. Then backtrack(start, remaining) tries each candidate from start onwards:
- Too big? If
candidates[i] > remaining, break: the list is sorted, so every candidate after it is too big as well. - Choose it: append it and subtract it from
remaining. - Explore with
backtrack(i, …): the samei, because a number may be used again. - Unchoose: pop it before trying the next candidate.
When remaining reaches 0 the path is a combination: record it and return.
Passing i rather than 0 is what keeps each combination unique: a call never goes back to a smaller candidate, so [2, 2, 3] is built once and [3, 2, 2] never is.
Interactive Visualization
ONE NODE PER CALL · THE LABEL IS THE CANDIDATE ADDED, THE NUMBER BELOW IS WHAT IS LEFT · RED = REJECTED BY THE BREAK
SORT THE CANDIDATES
left to make
7
depth
0
Answer
:
path
[]: nothing chosen
candidates (sorted)
result
Nothing found yet
Sort the candidates: 2, 3, 6, 7 is already in order. Start with an empty path and 7 left to make
Next
Choose 2: the path is [2] and 5 is left
candidates
Target
Every node is one call to backtrack. Its label is the candidate that call added, and the number under it is what is still left to make: a node reading 0 is a combination.
A red node is the one candidate a call looked at and rejected with break: its number goes negative. The candidates after it are never looked at, so they are not drawn at all: that missing part of the tree is the saving. Try Unsorted to see why the sort has to come first.
Dry Run
Tracing candidates = [2, 3], target = 5.
| # | Call | Path | What happens |
|---|---|---|---|
| 1 | backtrack(0, 5) |
[] |
2 ≤ 5: choose 2 → backtrack(0, 3) |
| 2 | backtrack(0, 3) |
[2] |
2 ≤ 3: choose 2 → backtrack(0, 1) |
| 3 | backtrack(0, 1) |
[2, 2] |
2 > 1: break, 3 is never tried. Return |
| 4 | backtrack(0, 3) |
[2] |
Pop 2. 3 ≤ 3: choose 3 → backtrack(1, 0) |
| 5 | backtrack(1, 0) |
[2, 3] |
Nothing left: record [2, 3], return |
| 6 | backtrack(0, 3) |
[2] |
Pop 3. Loop done: return |
| 7 | backtrack(0, 5) |
[] |
Pop 2. 3 ≤ 5: choose 3 → backtrack(1, 2) |
| 8 | backtrack(1, 2) |
[3] |
3 > 2: break. Return |
| 9 | backtrack(0, 5) |
[] |
Pop 3. Loop done: return |
Result: [[2, 3]]. Row 8 starts at index 1, so it never goes back to 2, which is why [3, 2] is not produced as well.
Edge Cases & Common Mistakes
Watch out for these pitfalls:
Reuse vs. move on: recurse with
ito allow the same number again. Passingi + 1silently solves a different problem (Combination Sum II, each number once).The break needs the sort:
breakis only safe because every later candidate is at least as big. On unsorted input it would skip combinations that exist: usecontinuethere, or sort first as this solution does.No combination at all:
[2]with target 1 returns[]: the root rejects its only candidate and records nothing. Return the empty list, not[[]].Zero and negatives: LeetCode guarantees candidates of at least 2. A 0 would let a path grow forever without changing
remaining, and a negative would break the pruning argument.
Solution
One algorithm in all six languages: the lines light up as the figure above steps.
Combination Sum Code
1var combinationSum = function(candidates, target) {2 candidates.sort((a, b) => a - b);3 const result = [];4 const path = [];56 const backtrack = (start, remaining) => {7 if (remaining === 0) {8 result.push([...path]);9 return;10 }11 for (let i = start; i < candidates.length; i++) {12 if (candidates[i] > remaining) {13 break;14 }15 path.push(candidates[i]);16 backtrack(i, remaining - candidates[i]);17 path.pop();18 }19 };2021 backtrack(0, target);22 return result;23};
Combination Sum in Python, Java, C++, TypeScript and C
Combination Sum in Python
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
result = []
path = []
def backtrack(start: int, remaining: int) -> None:
if remaining == 0:
result.append(path[:])
return
for i in range(start, len(candidates)):
if candidates[i] > remaining:
break
path.append(candidates[i])
backtrack(i, remaining - candidates[i])
path.pop()
backtrack(0, target)
return resultCombination Sum in Java
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
backtrack(candidates, 0, target, path, 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));
return;
}
for (int i = start; i < candidates.length; i++) {
if (candidates[i] > remaining) {
break;
}
path.add(candidates[i]);
backtrack(candidates, i, remaining - candidates[i], path, result);
path.remove(path.size() - 1);
}
}
}Combination Sum in C++
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> result;
vector<int> path;
backtrack(candidates, 0, target, path, result);
return result;
}
private:
void backtrack(vector<int>& candidates, int start, int remaining, vector<int>& path, vector<vector<int>>& result) {
if (remaining == 0) {
result.push_back(path);
return;
}
for (int i = start; i < (int)candidates.size(); i++) {
if (candidates[i] > remaining) {
break;
}
path.push_back(candidates[i]);
backtrack(candidates, i, remaining - candidates[i], path, result);
path.pop_back();
}
}
};Combination Sum in TypeScript
function combinationSum(candidates: number[], target: number): number[][] {
candidates.sort((a, b) => a - b);
const result: number[][] = [];
const path: number[] = [];
const backtrack = (start: number, remaining: number): void => {
if (remaining === 0) {
result.push([...path]);
return;
}
for (let i = start; i < candidates.length; i++) {
if (candidates[i] > remaining) {
break;
}
path.push(candidates[i]);
backtrack(i, remaining - candidates[i]);
path.pop();
}
};
backtrack(0, target);
return result;
}Combination Sum in C
int compare(const void* a, const void* b) {
return *(const int*)a - *(const int*)b;
}
void backtrack(int* candidates, int candidatesSize, int start, int remaining, int* path, int* pathSize, int** result, int* returnSize, int* returnColumnSizes) {
if (remaining == 0) {
result[*returnSize] = malloc((*pathSize > 0 ? *pathSize : 1) * sizeof(int));
for (int k = 0; k < *pathSize; k++) {
result[*returnSize][k] = path[k];
}
returnColumnSizes[*returnSize] = *pathSize;
(*returnSize)++;
return;
}
for (int i = start; i < candidatesSize; i++) {
if (candidates[i] > remaining) {
break;
}
path[(*pathSize)++] = candidates[i];
backtrack(candidates, candidatesSize, i, remaining - candidates[i], path, pathSize, result, returnSize, returnColumnSizes);
(*pathSize)--;
}
}
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
qsort(candidates, candidatesSize, sizeof(int), compare);
int capacity = 150; // LeetCode guarantees fewer than 150 combinations
int** result = malloc(capacity * sizeof(int*));
*returnColumnSizes = malloc(capacity * sizeof(int));
int* path = malloc(target * sizeof(int));
*returnSize = 0;
int pathSize = 0;
backtrack(candidates, candidatesSize, 0, target, path, &pathSize, result, returnSize, *returnColumnSizes);
free(path);
return result;
}More practice: DSA interview questions with answers · patterns cheat sheet
