# Program to find the sum of the subsets of an array

## Program to find the sum of the subsets of an array

Given an array , Write a Program to find the sum of the subsets of an array .

Sample input:-

```3
1 2 3```

Sample output:-

`0 3 2 5 1 4 3 6`

## Program to find the sum of the subsets of an array

The objective of the code is to find the sum of the subsets of an array .For finding the sum of the subsets first we have to generate all the subsets of array. So for how to generate all the subsets check this Program to generate all the subsets of an array  After generating all the subsets we will find the sum of all the subsets .

## Code:-

```#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> subsets;

// Function for generates subsets
void generateSubsets(vector<int>&subset,int i,vector<int>&nums)
{
if(i==nums.size())
{
subsets.push_back(subset);
return ;
}
// ith element is not included
generateSubsets(subset,i+1,nums);

subset.push_back(nums[i]);

// include the other elements
generateSubsets(subset,i+1,nums);

// pop back the ith element
subset.pop_back();
}

// function for finding the sum of the subsets
void findSumOfSubsets(vector<int>&subset,int i,vector<int>&nums)
{
generateSubsets(subset,i,nums);
for(auto sub:subsets)
{
int sum=0;
for(auto ele:sub)
{
sum+=ele;
}
cout<<sum<<" ";
}
}
//Driver function
int main()
{
vector<int>arr,empty;
int n,ele;
cout<<"Enter the number of element"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ele;
arr.push_back(ele);
}
findSumOfSubsets(empty,0,arr);

return 0;
}
```

Output:-

```Enter the number of element
3
1 2 3

0 3 2 5 1 4 3 6```

Explanation:-

Here in this code first we find all the subsets and then after find the sum of the subsets . for generating all the subsets we create a function generateSubsets()  by using this function generate all the subsets and store it in a global vector Subsets ,after that we will  find the sum of the subsets by the help of the function FindSumOfSubsets() . For better understand how to generate all the subsets of an array check this Program to generate all the subsets of an array