Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 24 Sep, 2020

Moderate

```
The order of subsets is not important.
The order of elements in a particular subset should be in increasing order of the index.
```

```
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.
```

```
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.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
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.
```

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:

- 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.
- 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.

The idea is to denote every subset as a binary representation of a positive integer. For example, let the size of a given array is 3 and an integer 5, which has a binary representation ‘101’, here ‘101’ means we take a subset which has elements 1st and 3rd(1 means include the element and 0 means not include the element). LSB of '101' represents the first element of the array and MSB represents the last element of the array.

Similarly, '111' means we have taken all the 3 elements in our subset.

So, if we have ‘n’ elements we need to have an integer that has its binary representation ‘n’ bits long, which is (2 ^ n) - 1. So, every integer from 0 to (2 ^ n) - 1 represents a different subset.

For checking, if an ith element is present in a subset or not we can say that if the ith bit from LSB is set then the ith element is present in a subset otherwise not.

- Declare a 2-d vector 'ans' which stores all possible subsets which sum up to ‘K’.
- Run a loop from 0 to (2 ^ n) - 1 (say iterator ‘i’)
- Initialize a variable 'sum'=0, which stores the sum of a particular subset and a vector ‘temp’ to store the subset.
- Iterate over the elements of the given array (say iterator ‘j’) and check to see whether an element is present in the current subset or not. If it is present then update sum as sum += num[j], add element in ‘temp’.

- If ‘sum’ is ‘K’
- Insert 'temp' in 'ans' vector.

- Initialize a variable 'sum'=0, which stores the sum of a particular subset and a vector ‘temp’ to store the subset.
- Return 'ans'.

Similar problems

Longest Subarray With Zero Sum

Moderate

Posted: 3 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Ninja And The Strictly Increasing Array

Moderate

Posted: 27 Nov, 2022

Negative To The End

Easy

Posted: 16 Dec, 2022

Find Duplicate in Array

Easy

Posted: 5 Jun, 2023