Last Updated: 21 Jan, 2022

Subset Sum

Easy
Asked in companies
AmazonGoldman SachsSAP Labs

Problem statement

You are given an array 'nums' of ‘n’ integers.


Return all subset sums of 'nums' in a non-decreasing order.


Note:
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'.


For example
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].
Input Format :
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'.
Output Format :
Return the sum of all the subsets of 'nums' in non-decreasing order.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.

Approaches

01 Approach

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. 

 

Here is the algorithm:

  • subset function:
    • If i is equal to the size of the “num” vector
      • Insert the “sum” in the “ans” vector.
      • Return.
    • subset(i+1, sum+num[i], num, ans)
    • subset(i+1, sum, num, ans)
  • given function:
    • Declare a vector “ans” which stores all possible subset sums. It will pass by reference in the “subset” function.
    • subset(0, 0, num, ans).
    • Sort the “ans” vector.
    • Return “ans”.

02 Approach

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.

 

Here is the algorithm :

  • Declare an integer ‘n’ and initialize it with the size of the given array.
  • Declare an vector “ans” which stores all possible subset sums.
  • 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.
      • 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]
    • Insert “sum” in “ans” vector.
  • Sort the “ans” vector.
  • Return “ans”.