You are given an array 'ARR' of 'N' distinct positive integers. You are also given a non-negative integer 'B'.
Your task is to return all unique combinations in the array whose sum equals 'B'. A number can be chosen any number of times from the array 'ARR'.
Elements in each combination must be in non-decreasing order.
Let the array 'ARR' be [1, 2, 3] and 'B' = 5. Then all possible valid combinations are-
(1, 1, 1, 1, 1)
(1, 1, 1, 2)
(1, 1, 3)
(1, 2, 2)
(2, 3)
Then the first line contains two space-separated integers, 'N' and 'B', denoting the number of elements in the array and the target sum, respectively.
The second line of each test case contains 'N' space-separated integers representing the elements of the array 'ARR'.
Output Format :
The only line will contain 'Yes', if the answer is correct. Else, it will contain 'No'.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
3 8
2 3 5
Yes
All possible valid combinations are:
2 2 2 2
2 3 3
3 5
3 5
1 2 3
Yes
1 <= 'N' <= 15
1 <= 'B' <= 20
1 <= 'ARR[i]' <= 20
Time Limit: 1sec
Try to think about a recursive solution
Approach:
Algorithm:
O(2^N), where N denotes the number of elements of the array ‘ARR’.
Since we are recursing over all possible combinations of the array ‘ARR’, the time complexity will be O(2^N).
O(N*2^N), where N denotes the number of elements of the array ‘ARR’.
The space complexity due to the recursion stack will be O(2^N). For each possible state, we store all possible elements in ‘currList’. Hence, overall space complexity will be O(N*2^N).