Medium Accuracy: 50.0%
Submissions: 19668 Points: 4
Given an array of integers and a sum B, find all unique combinations in the array where the sum is equal to B. The same number may be chosen from the array any number of times to make B.
Note: 1. All numbers will be positive integers. 2. Elements in a combination (a1, a2, …, ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak). 3. The combinations themselves must be sorted in ascending order.
Your task is to complete the function combinationSum() which takes the array A and a sum B as inputs and returns a list of list denoting the required combinations in the order specified in the problem description. The printing is done by the driver's code. If no set can be formed with the given set, then "Empty" (without quotes) is printed.
Expected Time Complexity: O(X2 * 2N), where X is average of summation B/arri for every number in the array. Expected Auxiliary Space: O(X * 2N)
1 <= N <= 30
1 <= A[i] <= 20
1 <= B <= 100