Topics/Backtracking/Combination sum

Pattern

Combination sum

Combination sum is a reusable approach under Backtracking. Once you recognize the shape of the input and the ask, this pattern gives you a standard plan instead of inventing a new algorithm from scratch.

BacktrackingAdvancedPattern

When to use it

Reach for Combination sum when a backtracking problem matches the cues in this lesson — especially after you’ve ruled out a brute-force nested scan.

How it works

  1. Restate the Backtracking problem in one sentence and name the constraint that Combination sum exploits.
  2. Sketch the invariant Combination sum maintains (what stays true after each step).
  3. Walk a tiny example by hand, writing the state after every update.
  4. Translate that walk into code in your interview language, then list edge cases.

Solution

Reference implementation in C#, Python, Java, JavaScript, and C++.

Exact solutionCombination sum

Why interviewers care

Interviewers grade pattern recognition. Naming Combination sum early shows you can classify the problem inside Advanced.

Drill Combination sum with Backtracking problems — filter the bank and solve 2–3 Easy/Medium items while saying the invariant out loud.

Watch-outs

  • !Forgetting to unchoose
  • !Duplicate branches
  • !Mutating shared lists without copy on save

Practice this

Related Backtracking problems — open any one and apply Combination sum.

Search bank →

More under Backtracking

← Back to Backtracking lesson