|
| 1 | +# 39. Combination Sum |
| 2 | + |
| 3 | +**Difficulty:** *Medium* |
| 4 | +**Category:** *Backtracking, Recursion* |
| 5 | +**Leetcode Link:** [Problem Link](https://leetcode.com/problems/combination-sum/) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 📝 Introduction |
| 10 | + |
| 11 | +*Given an array of distinct integers and a target, the task is to return all unique combinations where the chosen numbers sum up to the target. Each number in the array may be used an unlimited number of times.* |
| 12 | + |
| 13 | +*Constraints typically include:<br> |
| 14 | +- All input numbers are distinct positive integers.<br> |
| 15 | +- Each number may be used any number of times in a combination.<br> |
| 16 | +- Total number of unique combinations is less than 150.* |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 💡 Approach & Key Insights |
| 21 | + |
| 22 | +*The core idea is to explore all valid combinations recursively using the "pick / not pick" technique:<br> |
| 23 | +- At each step, either pick the current number and stay at the same index (since it can be reused), or skip it and move to the next index.<br> |
| 24 | +- Maintain a running sum and a temporary combination list.<br> |
| 25 | +- If the running sum becomes 0, we’ve found a valid combination and we save it.<br> |
| 26 | +- Backtrack after each recursive call to try new combinations.* |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 🛠️ Breakdown of Approaches |
| 31 | + |
| 32 | +### 1️⃣ Recursive Backtracking (Pick / Non-pick) |
| 33 | + |
| 34 | +- **Explanation:** *We recursively explore all combinations starting from index 0. For each number, we can choose to either include it (and stay at the same index) or exclude it (move to next index). Whenever target becomes 0, we add the current path to the result.* |
| 35 | +- **Time Complexity:** *O(2^t) – where t = target value. Actual complexity depends on pruning.* |
| 36 | +- **Space Complexity:** *O(k * x) – where x is the number of valid combinations, k is average length.* |
| 37 | +- **Example/Dry Run:** |
| 38 | + |
| 39 | +```plaintext |
| 40 | +Input: [2,3,6,7], target = 7 |
| 41 | +Sorted: [2,3,6,7] |
| 42 | +
|
| 43 | +Start at index 0: |
| 44 | +- Pick 2 → [2], target = 5 |
| 45 | + - Pick 2 → [2,2], target = 3 |
| 46 | + - Pick 2 → [2,2,2], target = 1 |
| 47 | + - Pick 2 → [2,2,2,2], target = -1 (backtrack) |
| 48 | + - Skip 2, pick 3 → [2,2,3], target = 0 → ✅ |
| 49 | + - Skip 2, pick 3 → [2,3], target = 2 |
| 50 | + - Pick 3 again → target = -1 (backtrack) |
| 51 | +- Pick 7 → [7], target = 0 → ✅ |
| 52 | +
|
| 53 | +Output: [[2,2,3], [7]] |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## 📊 Complexity Analysis |
| 59 | + |
| 60 | +| Approach | Time Complexity | Space Complexity | |
| 61 | +| ------------------- | --------------- | ---------------- | |
| 62 | +| Recursive Backtrack | O(2^t) | O(k × x) | |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## 📉 Optimization Ideas |
| 67 | + |
| 68 | +*Although the recursion tree explores all combinations, sorting the input and pruning branches where target < candidate[i] can improve efficiency. Additionally, memoization can be applied but is typically unnecessary within the constraint of <150 combinations.* |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## 📌 Example Walkthroughs & Dry Runs |
| 73 | + |
| 74 | +```plaintext |
| 75 | +Example 1: |
| 76 | +Input: [2,3,6,7], target = 7 |
| 77 | +
|
| 78 | +Recursive Tree: |
| 79 | +Start with [] |
| 80 | +Pick 2: [2] → target=5 |
| 81 | +Pick 2 again: [2,2] → target=3 |
| 82 | +Pick 2 again: [2,2,2] → target=1 |
| 83 | +Pick 2 again → exceeds → backtrack |
| 84 | +Backtrack and pick 3 → [2,2,3] → ✅ target=0 |
| 85 | +
|
| 86 | +Pick 7: [7] → ✅ |
| 87 | +
|
| 88 | +Output: [[2,2,3],[7]] |
| 89 | +
|
| 90 | +Example 2: |
| 91 | +Input: [2], target = 1 |
| 92 | +
|
| 93 | +Only choice: 2 > 1 → cannot pick → return [] |
| 94 | +
|
| 95 | +Output: [] |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## 🔗 Additional Resources |
| 101 | + |
| 102 | +- [Backtracking explanation](https://medium.com/upsolve-digest/template-for-backtracking-problems-part1-the-basics-75f744cab925) |
| 103 | +- [Python recursion guide](https://realpython.com/python-thinking-recursively/) |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +Author: Neha Amin <br> |
| 108 | +Date: 19/07/2025 |
0 commit comments