Backtracking
Medium
Combination Sum
Find all unique combinations that add up to a target sum (unlimited use of candidates).
10 min read
Solve on LeetCodeProblem 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
This is a classic backtracking problem. At each step, we have choices:
- Include the current candidate number again (and stay at same index
i). - Skip the current candidate and move to the next (
i + 1).
Pruning:
- If
currentSum > target, stop (backtrack). - If
currentSum == target, add to result.
Interactive Visualization
Step 1 / 1
Visualization coming soon...
Initializing...
1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
- Dry Run
- Edge Cases & Common Mistakes
- Solution
- Complexity Analysis
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
