Repository containing solution for #SdeSheetChallenge by striver
Given a list arr of N integers, print sums of all subsets in it.
last+1
(N) index then we can add the sum in the ans.void recFunc(int ind, int sum, vector<int> &arr, int N, vector<int> &sumArr)
, Where only ind and sum will change in almost every recursive call and other remain same.class Solution {
private:
void subsetSumsRec(int ind, int sum, vector<int> &arr, int N, vector<int> &sumArr)
{
// Base condition
if (ind == N) {
sumArr.push_back(sum);
return;
}
// pick element - (ind+1,sum+arr[ind])
subsetSumsRec(ind + 1, sum + arr[ind], arr, N, sumArr);
// not pick element - (ind+1,sum)
subsetSumsRec(ind + 1, sum, arr, N, sumArr);
}
public:
vector<int> subsetSums(vector<int> arr, int N)
{
vector<int> sumArr;
subsetSumsRec(0, 0, arr, N, sumArr);
sort(sumArr.begin(), sumArr.end());
return sumArr;
}
};