6.Combination Sum and Combination Sum II

Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

Each number in candidates may only be used once in the combination.

Note: The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8
Output: 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]

Example 2:

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

Solution : (Backtracking)

class Solution
{
public:
    vector<vector<int>> res;
    vector<int> v;

    void subSum(vector<int> &nums, int sum, int pos)
    {

        if (sum<0)
        {
            return;
        }

        if (sum == 0)
        {
            res.push_back(v);
            return;
        }

        for (int i = pos; i < nums.size(); i++)
        {
            if(i>pos && nums[i] == nums[i-1]){
                continue;
            }
            v.push_back(nums[i]);
            subSum(nums, sum - nums[i], i + 1);
            v.pop_back();
        }

        return;
    }

    vector<vector<int>> combinationSum2(vector<int> &candidates, int target)
    {
        sort(candidates.begin(), candidates.end());
        subSum(candidates, target, 0);
        return res;
    }
};

Combination Sum

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.

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: []


Example 4:
Input: candidates = [1], target = 1
Output: [[1]]


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

Solution: (Recursion)

class Solution
{
public:
    
    vector<vector<int>> res;
    vector<int> v;

    void subsetSum(vector<int> &nums, int sum, int pos)
    {
        
        if (sum == 0)
        {
            res.push_back(v);
            return;
        }
        
        if (sum < 0 || pos>=nums.size())
        {
            return;
        }
        
        v.push_back(nums[pos]);
        subsetSum(nums,sum - nums[pos],pos);
        
        v.pop_back();
        subsetSum(nums,sum,pos+1);
        return;
        
    }
    
    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        subsetSum(candidates, target, 0);
        return res;
    }
};

Solution: (Backtracking)

class Solution
{
public:
    vector<vector<int>> res;
    vector<int> v;

    void subsetSum(vector<int> &nums, int sum, int pos)
    {
    
        if (sum == 0)
        {
            res.push_back(v);
            return;
        }

        if (sum < 0)
        {
            return;
        }

        for (int i = pos; i < nums.size(); i++)
        {
            v.push_back(nums[i]);
            subsetSum(nums, sum - nums[i], i);
            v.pop_back();
        }

        return;
    }

    vector<vector<int>> combinationSum(vector<int> &candidates, int target)
    {
        subsetSum(candidates, target, 0);
        return res;
    }
};

Combination Sum III

Find all valid combinations of k numbers that sum up to n such that the following conditions are true:

  • Only numbers 1 through 9 are used.

  • Each number is used at most once.

Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.

Example 2:
Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.


Example 3:
Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations. [1,2,1] is not valid because 1 is used twice.


Example 4:
Input: k = 3, n = 2
Output: []
Explanation: There are no valid combinations.


Example 5:
Input: k = 9, n = 45
Output: [[1,2,3,4,5,6,7,8,9]]
Explanation:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
​​​​​​​There are no other valid combinations.

Solution: (Recursion)

class Solution
{
public:
    vector<vector<int>> res;
    
    void findSubset(int k, int start, int sum, int numCount, vector<int> &v)
    {
        if (sum == 0 && numCount == k)
        {
            res.push_back(v);
            return;
        }

        if (sum < 0)
        {
            return;
        }

        if (numCount > k)
        {
            return;
        }

        for (int i = start; i < 10; i++)
        {
            v.push_back(i);
            findSubset(k, i + 1, sum - i, numCount + 1, v);
            v.pop_back();
        }

        return;
    }
    
    vector<vector<int>> combinationSum3(int k, int n)
    {
        vector<int> v;
        findSubset(k, 1, n, 0, v);
        return res;
    }
};

Combination Sum IV

Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.

The answer is guaranteed to fit in a 32-bit integer.

Example 1:

Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.

Example 2:

Input: nums = [9], target = 3
Output: 0

Solution: (Recursion + memo)

class Solution
{
public:
    int checkSubset(vector<int> &nums, int target, vector<int> &dp)
    {
        int val = 0;

        if (target == 0)
        {
            return 1;
        }

        if (target < 0)
        {
            return 0;
        }
        
        if (dp[target] != -1)
        {
            return dp[target];
        }

        for (int i = 0; i < nums.size(); i++)
        {
            val += checkSubset(nums, target - nums[i], dp);
        }

        dp[target] = val;

        return val;
    }

    int combinationSum4(vector<int> &nums, int target)
    {
        vector<int> dp(target + 1, -1);
        int count = checkSubset(nums, target, dp);
        return count;
    }
};

Last updated