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
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 = 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
Solution
This problem can be solved using a technique called backtracking. Here is a Python solution:
def combinationSum(candidates, target):
def backtrack(remain, comb, start):
if remain == 0:
result.append(list(comb))
return
elif remain < 0:
return
for i in range(start, len(candidates)):
comb.append(candidates[i])
backtrack(remain - candidates[i], comb, i)
comb.pop()
result = []
backtrack(target, [], 0)
return result
Here's how it works:
-
We define a helper function
backtrackthat takes the remaining sum after subtracting the current number, the current combination, and the current start index. -
If the remaining sum is 0, we have found a valid combination, so we add it to the result list.
-
If the remaining sum is less than 0, we have exceeded the target, so we return without adding the combination to the result list.
-
We iterate over the candidates starting from the current index. For each candidate, we add it to the current combination and recursively call
backtrackwith the updated remaining sum and the current index. We then remove the candidate from the combination to backtrack and try the next candidate. -
Finally, we call
backtrackwith the initial target, an empty combination, and a start index of 0, and return the result list.
Similar Questions
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]]
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).
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
Write a Python class to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number.Note: There will be one solution for each input and do not use the same element twice.Sample Test CasesTest Case 1:Expected Output:Enter·comma-separated·numbers:·2,5,1,5,6,0Enter·target·value:·10index1=1,·index2=3Test Case 2:Expected Output:Enter·comma-separated·numbers:·3,4,5,6,2,9Enter·target·value:·9index1=0,·index2=3Test Case 3:Expected Output:Enter·comma-separated·numbers:·9,8,1,4,5Enter·target·value:·4No·valid·indices·found·for·the·target·value.
Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].You may return the answer in any order. Example 1:Input: n = 4, k = 2Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]Explanation: There are 4 choose 2 = 6 total combinations.Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.Example 2:Input: n = 1, k = 1Output: [[1]]Explanation: There is 1 choose 1 = 1 total combination. Constraints:1 <= n <= 201 <= k <= n
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.