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
Recommended post:-
- Primary test
- Sum or difference
- point and line
Wipro :-
- Update the booking ID | Wipro previous year question paper solution
- Pages in PDF
- Find the location id
- Find the odd digits
- Find the Product ID
Infytq :-
Key Points;-
Hackerrank:-
- Python : missing characters : hackerrank solution
- Python : string transformation | Hackerrank solution
- Active Traders certification test problem | Hackerrank Solution
- Usernames changes certification test problem | Hackerrank Solution
- string Representation of objects certification test hackerrank solution
- Average Function | hackerrank certification problem solution
C-tutorial:-
- Micros in C
- Pointer in c
- Function declaration
- Types of user define function
- return type of function
- 2D array
See more:-
- c program to convert specified days into years weeks and days
- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation