Coding interview problems › Backtracking
🧩 Backtracking Medium
Combination Sum
Combination Sum explained: backtracking with reuse and pruning to list every combination that adds to a target, tested Python, pitfalls and a live visual.
Also searched as: combination sum backtracking · find all combinations that sum to target · combination sum with repetition
Try it live Free
Backtracking: Subsets, Permutations & Pruning
Switch on pruning in the Backtracking lab and watch the explored-node counter collapse while the answers stay exactly the same, the same effect the early break has here.
Also useful: Coin Change (DP)
The problem
Given a list of distinct positive integers and a target, return every unique combination of the numbers that adds up to target. A number may be used any number of times. Combinations that differ only in order count as the same.
The key insight
Build combinations in non-decreasing index order so each combination is generated once. After choosing candidates[i], recurse with the same i (the number can be reused) and a smaller remaining target. With the candidates sorted, once a number is bigger than what remains, every later number is too, so you can prune the whole loop.
Step-by-step approach
- Sort the candidates.
- Define
dfs(start, remain). Ifremain == 0, record a copy ofpath. - Loop
ifromstart. Ifcandidates[i] > remain, break (pruning). - Choose
candidates[i], calldfs(i, remain - candidates[i])(sameiallows reuse), then pop. - Call
dfs(0, target).
Python solution
def combination_sum(candidates, target):
candidates = sorted(candidates)
out, path = [], []
def dfs(start, remain):
if remain == 0:
out.append(path[:])
return
for i in range(start, len(candidates)):
c = candidates[i]
if c > remain:
break # sorted, so the rest are too big
path.append(c)
dfs(i, remain - c) # i, not i + 1: reuse allowed
path.pop()
dfs(0, target)
return outComplexity
With target T and smallest candidate m, the recursion is at most T/m deep and branches up to n ways, so the worst case is exponential. Pruning cuts most branches in practice.
Common mistakes
- Recursing with
i + 1, which forbids reuse (that is Combination Sum II's rule). - Restarting the loop from 0 in each call, which produces
[2, 3, 2]and[3, 2, 2]as separate answers. - Forgetting to sort before using
break; without sorting, pruning skips valid candidates.
Frequently asked questions
What is the time and space complexity of Combination Sum?
Exponential in the worst case, roughly O(n^(T/m)) where T is the target and m the smallest candidate. The recursion depth and path are O(T/m).
How is Combination Sum II different?
Each number may be used once and the input can contain duplicates. Recurse with i + 1, sort the input, and skip candidates[i] when i > start and it equals candidates[i - 1].
How is this related to Coin Change II?
Coin Change II counts the same combinations without listing them, so a DP over amounts is enough. Combination Sum must list every combination, so backtracking is natural.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Backtracking Backtracking: Subsets, Permutations & Pruning lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.