#include<bits/stdc++.h>
using namespace std;
void disp_l( vector<int> &l) {
cout<<"\n";
for(auto x:l) {
cout<<x<<" ";
}
cout<<endl;
}
void disp(vector<vector<int>> &ans) {
for(int i=0; i< ans.size(); i++) {
int sum =0;
for(int j=0; j<ans[i].size(); j++) {
cout<< ans[i][j] <<" ";
sum += ans[i][j];
}
cout<<" "<<sum;
cout<<endl;
}
}
//subsetSum(0, n/2,arr, layer, ans, sum);
void subsetSum(int i, int n,vector<int> &arr, vector<int> &layer, vector<vector<int>> &ans, int sum) {
if( i== n) {
ans.push_back(layer);
//disp_l(layer);
return;
}
//pick
layer.push_back(arr[i]);
sum+=arr[i];
subsetSum(i+1, n,arr, layer, ans, sum);
layer.pop_back();
sum = sum- arr[i];
subsetSum(i+1, n,arr, layer, ans, sum);
}
int main(){
vector<int> arr = {45, 34, 4, 12, 5, 23};
int n = sizeof(arr)/ sizeof(arr[0]);
//cout<<n;
int sum =0;
vector<int> layer;
vector<vector<int>> ans;
vector<vector<int>> ans1;
subsetSum(0, n/2,arr, layer, ans, sum);
layer.clear();
disp(ans);
cout<<"\n NEW \n";
subsetSum(n/2,n, arr, layer, ans1, 0);
disp(ans1);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgZGlzcF9sKCAgdmVjdG9yPGludD4gJmwpIHsKICAgIGNvdXQ8PCJcbiI7CiAgICBmb3IoYXV0byB4OmwpIHsKICAgICAgICBjb3V0PDx4PDwiICI7CiAgICB9CiAgICBjb3V0PDxlbmRsOwp9Cgp2b2lkIGRpc3AodmVjdG9yPHZlY3RvcjxpbnQ+PiAmYW5zKSB7Cglmb3IoaW50IGk9MDsgaTwgYW5zLnNpemUoKTsgaSsrKSB7CgkJaW50IHN1bSA9MDsKCQlmb3IoaW50IGo9MDsgajxhbnNbaV0uc2l6ZSgpOyBqKyspIHsKCQkJY291dDw8IGFuc1tpXVtqXSA8PCIgIjsKCQkJc3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