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 = 8Output: [[1,1,6],[1,2,5],[1,7],[2,6]]Example 2:Input: candidates = [2,5,2,1,2], target = 5Output: [[1,2,2],[5]]
Question
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 = 8Output: [[1,1,6],[1,2,5],[1,7],[2,6]]Example 2:Input: candidates = [2,5,2,1,2], target = 5Output: [[1,2,2],[5]]
Solution
This problem can be solved using a technique called backtracking. Here are the steps to solve this problem:
-
First, sort the array of candidates. This will help to avoid duplicates and to stop the recursion early.
-
Create a function, let's call it
backtrack, that will take the current combination, the sum of the current combination, and the start position for the next candidate. -
In the
backtrackfunction, check if the sum of the current combination is equal to the target. If it is, add the current combination to the result and return. -
If the sum of the current combination is greater than the target, return without adding the current combination to the result. This is because, since the candidates are sorted, if the current combination exceeds the target, any further combinations will also exceed the target.
-
If the sum of the current combination is less than the target, iterate over the candidates starting from the start position. For each candidate, add it to the current combination and call
backtrackwith the new combination, the new sum, and the next position as the start position. This is because each number in candidates may only be used once in the combination. -
After calling
backtrackfor a candidate, remove it from the current combination to backtrack. -
To avoid duplicates, if a candidate is the same as the previous candidate, skip it.
-
Finally, call
backtrackwith an empty combination, a sum of 0, and a start position of 0 to start the process.
Here is a Python solution for the problem:
def combinationSum2(candidates, target):
def backtrack(comb, start, target):
if target == 0:
result.append(list(comb))
return
for i in range(start, len(candidates)):
if i > start and candidates[i] == candidates[i-1]:
continue
if candidates[i] > target:
break
comb.append(candidates[i])
backtrack(comb, i+1, target-candidates[i])
comb.pop()
candidates.sort()
result = []
backtrack([], 0, target)
return result
You can call the function with the candidates and the target as arguments, like this:
print(combinationSum2([10,1,2,7,6,1,5], 8))
print(combinationSum2([2,5,2,1,2], 5))
This will print:
[[1,1,6],[1,2,5],[1,7],[2,6]]
[[1,2,2],[5]]
which are the expected outputs.
Similar Questions
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 = 7Output: [[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 = 8Output: [[2,2,2,2],[2,3,3],[3,5]]Example 3:Input: candidates = [2], target = 1Output: [] Constraints:1 <= candidates.length <= 302 <= candidates[i] <= 40All elements of candidates are distinct.1 <= target <= 40
Given an array of integers arr, the length of the array n, and an integer k, find all the unique combinations in arr where the sum of the combination is equal to k. Each number can only be used once in a combination.Example 1:Input: n = 5, k = 7arr[] = { 1, 2, 3, 3, 5 }Output:{ { 1, 3, 3 }, { 2, 5 } }Explanation:1 + 3 + 3 = 72 + 5 = 7
bool combinations( int array[ ], int size, int target ); - Returns true if the total of any combination of elements in the array parameter equals the target value. In determining combinations, each element value should only be used once. For array of {2, 3, 4}, you could build target values of only 2, 3, 4, 5 (2+3), 6 (2+4), 7 (3+4) or 9 (2+3+4) only and not 12 (3+3+3+3) or 16 (4+4+4+4).
442. Find All Duplicates in an ArrayMediumTopicsCompaniesGiven an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.You must write an algorithm that runs in O(n) time and uses only constant extra space. Example 1:Input: nums = [4,3,2,7,8,2,3,1]Output: [2,3]Example 2:Input: nums = [1,1,2]Output: [1]Example 3:Input: nums = [1]Output: [] Constraints:n == nums.length1 <= n <= 1051 <= nums[i] <= nEach element in nums appears once or twice.
1. Two SumEasy53.1K1.8KCompaniesGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order. Example 1:Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].Example 2:Input: nums = [3,2,4], target = 6Output: [1,2]Example 3:Input: nums = [3,3], target = 6Output: [0,1] Constraints:2 <= nums.length <= 104
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.