Data Structures & Algorithms Backtracking
💡
Exercise 144

Combination Sum 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Find all unique combinations of candidates that sum to target. The same number can be used unlimited times.

candidates = [2, 3, 6, 7], target = 7 # [2, 2, 3] and [7] → 2 combinations

Backtracking: at each step, either take the current candidate again (pass same index) or move to the next candidate.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find all combinations summing to 7 from [2,3,6,7]. Print the count.
Backtrack(start, path, remaining). If remaining==0, add path. For each candidate from start, if candidate <= remaining, recurse.
🧪 Test Cases
Input
len(combinationSum([2,3,6,7], 7))
Expected
2
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.