Skip to main content
Medium Backtracking High frequency

Combination Sum

Open on LeetCode

Approach Summary

Backtrack with start index. Can reuse same element (don't advance start). Prune when sum exceeds target.

Full Solution & Approach

Unlimited reuse changes the backtracking guard: at each step you may pick the same index again, so recursion passes the current start index instead of start + 1. The helper tracks the running sum; when it equals the target, record the path; when it exceeds the target, prune. The key to avoiding duplicate combinations is the start index — an element can only be chosen at or after its position in the array, which prevents the same set in different orders. Iterate i from start to the end, add candidates[i], recurse with the same i (reuse allowed), then pop. Because the candidates are sorted in many solutions, an early break when candidates[i] exceeds the remaining target prunes the search space substantially, though it is not required for correctness.

The recursion tree is bounded by the target over the smallest coin — O(target^(n/min)) worst case, far less in practice with pruning. The path depth is at most target / min element — O(target / min) space.

Solution Code

Solution

def combination_sum(candidates: list[int], target: int) -> list[list[int]]:
    res = []

    def backtrack(start: int, path: list[int], total: int) -> None:
        if total == target:
            res.append(path[:])
            return
        if total > target:
            return
        for i in range(start, len(candidates)):
            path.append(candidates[i])
            backtrack(i, path, total + candidates[i])
            path.pop()

    backtrack(0, [], 0)
    return res

Edge Cases to Watch

  • No combination reaches the target — return []
  • A single candidate that divides the target — repeated copies are valid
  • Target 0 — the empty combination, [[]]
  • Candidates larger than the target — skipped

How to Recognize This Pattern

  • Unbounded element selection
  • Sum to target

Complexity Analysis

Time Complexity

O(n^(T/M))

Space Complexity

O(T/M)

Tags

Array Backtracking

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee