The Indian Engineer

Problem 39 Combination Sum

Posted on 7 mins

Array Backtracking

Problem Statement

Link - Problem 39

Question

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

Example 2:

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3:

Input: candidates = [2], target = 1
Output: []

Constraints:

Solution

class Solution {
public:
    vector<vector<int>> ans;
    void solve(int i, vector<int>& arr, vector<int>& temp, int target)
    {
        if(target == 0)
        {
            ans.push_back(temp);
            return;
        }

        if(target < 0)
            return;

        if(i == arr.size())
            return;
        solve(i + 1, arr, temp, target);

        temp.push_back(arr[i]);
        solve(i, arr, temp, target - arr[i]);
        temp.pop_back();

    }
    vector<vector<int>> combinationSum(vector<int>& arr, int target) {
        ans.clear();
        vector<int> temp;
        solve(0, arr, temp, target);
        return ans;
    }
};

Complexity Analysis

| Algorithm    | Time Complexity | Space Complexity |
| ------------ | --------------- | ---------------- |
| Backtracking | O(n 2^n)        | O(n)             |

Explanation

Intial Thoughts

To solve this problem, we need to think about how to break down the target number into smaller parts using the given candidates. Key points to consider are: the same number can be used unlimited times, the order of the numbers doesn’t matter, and we only need unique combinations. We can approach this by thinking of it as a tree where each node represents adding a number to our current sum, and we can prune branches that exceed the target. Additionally, considering the constraints that the number of unique combinations is less than 150 can help us focus on efficient exploration of possible combinations. We also need to think about how to keep track of the combinations we’ve already found to avoid duplicates. Lastly, an understanding of backtracking will be crucial to efficiently explore all possible combinations.

Intuitive Analysis

Intuitively, solving this problem involves visualizing a recursive process where we try adding each candidate number to our current sum, and then recursively try adding more numbers until we reach the target or exceed it. If we exceed the target, we stop exploring that branch. If we reach the target, we’ve found a valid combination. We need to think about how to implement this recursive process in a way that avoids redundant calculations and efficiently explores all possible combinations. This involves considering how to organize our recursive calls, how to keep track of the current combination being explored, and how to avoid exploring the same combination multiple times. By breaking down the problem into these smaller pieces and considering how they fit together, we can build an intuitive understanding of how to find all unique combinations that sum to the target.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.


Similar Questions:

Title URL Difficulty
Letter Combinations of a Phone Number https://leetcode.com/problems/letter-combinations-of-a-phone-number Medium
Combination Sum II https://leetcode.com/problems/combination-sum-ii Medium
Combinations https://leetcode.com/problems/combinations Medium
Combination Sum III https://leetcode.com/problems/combination-sum-iii Medium
Factor Combinations https://leetcode.com/problems/factor-combinations Medium
Combination Sum IV https://leetcode.com/problems/combination-sum-iv Medium
The Number of Ways to Make the Sum https://leetcode.com/problems/the-number-of-ways-to-make-the-sum Medium