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
Question
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
Solution
Sure, here is a Python solution for the problem:
def combinationSum(arr, n, k):
arr.sort()
temp = []
result = []
unique = []
total, j = 0, 0
findNumbers(arr, n, k, temp, total, j, result, unique)
return unique
def findNumbers(arr, n, k, temp, total, j, result, unique):
if total == k:
if sorted(temp) not in result:
result.append(sorted(temp))
unique.append(temp[:])
return
for i in range(j, n):
if (total + arr[i]) <= k:
temp.append(arr[i])
total += arr[i]
findNumbers(arr, n, k, temp, total, i+1, result, unique)
total -= temp.pop()
# Test the function
n = 5
k = 7
arr = [1, 2, 3, 3, 5]
print(combinationSum(arr, n, k))
This Python code defines a function combinationSum that sorts the input array and initializes some variables. It then calls the helper function findNumbers to find all combinations of numbers that add up to k. The helper function uses a recursive approach to find all combinations. If the current total equals k, it checks if the combination is unique and if so, adds it to the result. If the total is less than k, it continues to add the next number in the array to the total and calls itself with the updated total and array index. After each recursive call, it removes the last number added to the total to backtrack and find other combinations. The function finally returns all unique combinations.
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 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 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
Given an array A of integers and another number K. Find all the unique quadruple from the given array that sums up to K.Also note that all the quadruples which you return should be internally sorted, ie for any quadruple [q1, q2, q3, q4] the following should follow: q1 <= q2 <= q3 <= q4.
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.