Return Subsets Sum to K

Moderate
0/80
Average time to solve is 40m
114 upvotes
Asked in companies
MicrosoftSamsungSprinklr

Problem statement

Given an integer array 'ARR' of size 'N' and an integer 'K', return all the subsets of 'ARR' which sum to 'K'.

Subset of an array 'ARR' is a tuple that can be obtained from 'ARR' by removing some (possibly all) elements of 'ARR'.

Note :
The order of subsets is not important. 

The order of elements in a particular subset should be in increasing order of the index.
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first line of input contains an integer 'N', which denotes the size of the array.

The second line contains 'N' single-space separated integers representing the elements of the array.

The third line contains a single integer 'K', which denotes the integer to which the subsets should sum to.
Output Format:
For each test case, print single-space separated integers of a subset of 'ARR' having sum = 'K'. 

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'N' <= 16
- (10 ^ 6) <= ARR[i] <= (10 ^ 6)
- 16 * (10 ^ 6) <= 'K' <= 16 * (10 ^ 6)

Where ‘ARR[i]’ denotes the value for ‘ith’ element of the array ‘ARR’ and 'K' is the given sum.

Time Limit: 1 sec.
Sample Input 1:
3
2 4 6
6
Sample Output 1:
2 4
6
Explanation of the Sample Input 1:
For the array'ARR' = {2, 4, 6}, we can have subsets {}, {2}, {4}, {6}, {2, 4}, {2, 6}, {4, 6}, {2, 4, 6}. Out of these 8 subsets, {2, 4} and {6} sum to the given 'K' i.e. 6. 
Sample Input 2:
6 
5 -1 8 2 7 0
7
Sample Output 2:
-1 8 
-1 8 0 
5 2 
5 2 0 
7 
7 0 
Hint

Try to do as asked in the problem statement i.e generate all the subsets and for each subset check if it sums to ‘K’.

Approaches (2)
Brute Force

Recursively generate all the subsets and keep track of the sum of the elements in the current subset.

Subsets can be generated in the following way. For every element of the array, there are 2 options: 

  1. Include the element in the current subset : If we include the element in the current subset, then we decrease the value of ‘K’ by  the value of the element.
  2. Do not include the element in the current subset : There is no effect on the value of ‘K’ and we can simply move onto the next element.

In any step, if the value of ‘K’ becomes 0, then we have found a subset which sums to ‘K’. We store all these subsets and return them.

Time Complexity

O((2 ^ N) * N), where ‘N’ is the number of elements in the integer array ‘ARR’.

 

Total 2 ^ N subsets are possible for an integer array ‘ARR’ of size ‘N’, with a subset having a maximum size as ‘N’.

Space Complexity

O((2 ^ N) * N), where ‘N’ is the number of elements in the integer array ‘ARR’.

 

Total 2 ^ N subsets are possible for an integer array 'ARR' of size ‘N’, with a subset having a maximum size as ‘N’.

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Return Subsets Sum to K
Full screen
Console