Combination Sum

Find all combinations that sum to a target (can reuse elements).

Pattern

Backtrack with Reuse

This problem follows the Backtrack with Reuse pattern, commonly found in the Backtracking category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Try each candidate. Recurse with same index (reuse allowed). Backtrack if sum exceeds target.

Key Insight

Passing 'i' (not 'i+1') allows reuse of the same element. Sorting + early termination when candidate > target is the key optimization.

Step-by-step

  1. 1Sort candidates to help with pruning
  2. 2Use backtracking: at each step, try adding each candidate
  3. 3Pass the same index (not i+1) to allow reuse
  4. 4If the running sum exceeds target, stop exploring (prune)
  5. 5If the sum equals target, add the combination to results

Pseudocode

result = []
candidates.sort()
def backtrack(start, target, current):
    if target == 0:
        result.append(current[:])
        return
    for i in range(start, len(candidates)):
        if candidates[i] > target: break
        current.append(candidates[i])
        backtrack(i, target - candidates[i], current)
        current.pop()
backtrack(0, target, [])
return result
Complexity Analysis

Time Complexity

O(2ⁿ)

Space Complexity

O(target)
More Backtracking Problems

Master this pattern with YeetCode

Practice Combination Sum and similar Backtracking problems with flashcards. Build pattern recognition through active recall.

Practice this problem