Subset Sums | Practice | GeeksforGeeks

Given a list arr of N integers, print sums of all subsets in it.
 
Example 1:
Input:
N = 2
arr[] = {2, 3}
Output:
0 2 3 5
Explanation:
When no elements is taken then Sum = 0.
When only 2 is taken then Sum = 2.
When only 3 is taken then Sum = 3.
When element 2 and 3 are taken then 
Sum = 2+3 = 5.
Example 2:
Input:
N = 3
arr = {5, 2, 1}
Output:
0 1 2 3 5 6 7 8

Your Task:  
You don't need to read input or print anything. Your task is to complete the function subsetSums() which takes a list/vector and an integer N as an input parameter and return the list/vector of all the subset sums.
Expected Time Complexity: O(2N)
Expected Auxiliary Space: O(2N)
Constraints:
1 <= N <= 15
0 <= arr[i] <= 104
+1
premranjan880461 week ago
c++ recursive solution
 
   vector<int>v;   void solve(vector<int>arr,int sum, int i)   {       if(i==arr.size())       {           v.push_back(sum);           return;       }       solve(arr,sum,i+1);       solve(arr,sum+arr[i],i+1);   }      vector<int> subsetSums(vector<int> arr, int N)   {       solve(arr,0,0);       sort(v.begin(),v.end());              return v;   }
0
mamoonakhterrock20021 week ago
class Solution{
    ArrayList<Integer> subsetSums(ArrayList<Integer> arr, int N){
       
        // code here
      ArrayList < Integer > sumSubset = new ArrayList < > ();
        int n = N;
        func(0,0,arr,N,sumSubset);
        
        
        return sumSubset;
    }
    
    static void func(int ind, int sum, ArrayList < Integer > arr, int N, ArrayList < Integer > sumSubset){
        if(ind==N){
            sumSubset.add(sum);
            return;
        }
        
        func(ind+1,sum+arr.get(ind),arr,N,sumSubset);
        
        func(ind+1,sum,arr,N,sumSubset);
        
    }
}
0
manabghosh9241 week ago
vector<int> subsetSums(vector<int> arr, int N)   {       vector<int> res;       // Write Your Code here              res.push_back(0);       int M;       int i, j, sum=0;              for(j=0;j<N;j++)       {           M = res.size();           for(i=0;i<M;i++)           {               sum=arr[j]+res[i];               res.push_back(sum);               sum=0;           }       }       return res;          }
0
sakesai302 weeks ago
class Solution{   ArrayList<Integer> subsetSums(ArrayList<Integer> arr, int N){       ArrayList<Integer> mama = new ArrayList<>();       sub(0,arr,N,0,mama);       return mama;   }      static void sub(int index,ArrayList<Integer> arr,int N,int sum,ArrayList<Integer> mama){       if(index == N){           mama.add(sum);           return;       }       sum+=arr.get(index);       sub(index+1,arr,N,sum,mama);       sum-=arr.get(index);       sub(index+1,arr,N,sum,mama);   }
0
auntylover10823 weeks ago
Simple CPP Solution using recursion && backtracking
 
class Solution

{

public:

    void sum(int i,vector<int>v,int N,vector<int>arr,vector<int>&ans){

        if(i>=N){

            int a= accumulate(v.begin(),v.end(),0);

            ans.push_back(a);

            return;

            }

        //take it

        v.push_back(arr[i]);

        sum(i+1,v,N,arr,ans);

        //do not takeit

        v.pop_back();

        sum(i+1,v,N,arr,ans);

        

        

    }

    vector<int> subsetSums(vector<int> arr, int N)

    {

        vector<int>ans,v;

        sum(0,v,N,arr,ans);

        return ans;

        // Write Your Code here

        

    }

};
0
dipanshusharma93133 weeks ago
// java solution
class Solution{   ArrayList<Integer> sub(ArrayList<Integer> arr, int idx, int sum, ArrayList<Integer> di){       if(idx == arr.size()){           di.add(sum);           return di;       }       int a = arr.get(idx);       // to be       sub(arr, idx+1, sum+a, di);       // not to be       sub(arr, idx+1, sum, di);              return di;   }   ArrayList<Integer> subsetSums(ArrayList<Integer> arr, int N){       // code here       ArrayList<Integer> di = new ArrayList<Integer>();       sub(arr, 0, 0, di);       return di;   }}
+1
phantom3334 weeks ago
void solve(int id,vector<int> &arr,int sum,int n,vector<int> &ds){
    if(id==n){
        ds.push_back(sum);
        return;
    }
    sum+=arr[id];
    solve(id+1,arr,sum,n,ds);
    sum-=arr[id];
    solve(id+1,arr,sum,n,ds);
}
 
vector<int> subsetSums(vector<int> arr,int N){
    vector<int> ds;
    int sum=0;
    sort(ds.begin(),ds.end());
    solve(0,arr,sum,N,ds);
    return ds;
}
 
+3
shehzad874501 month ago
//solving this problem by IBH    //for more checkout  [Aaditya Verma] youtube channel   vector<int> res;   void solve(vector<int>arr, int sum){       //base condition       if(arr.size() == 0){           res.push_back(sum);           return;       }              int op1 = sum;       int op2 = sum;       op2 = sum + arr[0];       arr.erase(arr.begin() + 0);              solve(arr, op1);       solve(arr,op2);   }      vector<int> subsetSums(vector<int> arr, int N)   {       solve(arr,0);       return res;          }
0
akkeshri140420011 month ago
vector<int>res;
void solve(vector<int>arr,int indx,int op,int N){
   if(indx==N){
       res.push_back(op);
       return;
   }
   solve(arr,indx+1,op+arr[indx],N);
   solve(arr,indx+1,op,N);
}
   vector<int> subsetSums(vector<int> arr, int N)
   {
       // Write Your Code here
       solve(arr,0,0,N);
       return res;
   }
0
ksbsbisht1371 month ago
vector<int> res;   void solve(vector<int> arr,int N,int output) {       if(N==0) {           res.push_back(output);           return;       }       solve(arr,N-1,output+arr[N-1]);       solve(arr,N-1,output);   }   vector<int> subsetSums(vector<int> arr, int N) {       solve(arr,N,0);       return res;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.