


Here subset sum means sum of all elements of a subset of 'nums'. A subset of 'nums' is an array formed by removing some (possibly zero or all) elements of 'nums'.
Input: 'nums' = [1,2]
Output: 0 1 2 3
Explanation:
Following are the subset sums:
0 (by considering empty subset)
1
2
1+2 = 3
So, subset sum are [0,1,2,3].
The first line of input contains a single integer ‘n’, denoting the size of the array 'nums'.
The second line of input contains ‘n’ space-separated integers denoting elements of the array 'nums'.
Return the sum of all the subsets of 'nums' in non-decreasing order.
You do not need to print anything, it has already been taken care of. Just implement the given function.
The basic idea is to go over recursively to find every possible subset. For every element, we have two choices
1. Include the element in our subset.
2. Don’t include the element in our subset.
When we include an element in the set then it will contribute to the subset sum.
Let us define a function “subset( i, sum, num, ans)”, where ‘i’ is the current index of the array we are at, “sum” is the current sum of the current subset, “num” is the given vector, “ans” is the vector which stores the sum of all possible subsets.
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.
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum