Created
May 21, 2021 14:59
-
-
Save prasadwrites/49ca0dd433b81e7c73eb5385445663ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<vector> | |
| using namespace std; | |
| vector<int> bag = { 2, 8, -2 }; | |
| int sum = 6, index=0; | |
| int bagSize; | |
| bool flag = false; | |
| bool printSubsetSums(int index , vector<int> slate){ | |
| //basecase | |
| if(index == bagSize){ | |
| cout << endl; | |
| int subsetSum = 0; | |
| int sizeSlate = slate.size(); | |
| for (auto i : slate){ | |
| //cout << i << " "; | |
| subsetSum = subsetSum + i; | |
| } | |
| if(subsetSum == sum){ | |
| cout <<endl<<"sum found" <<endl; | |
| for (auto i : slate){ | |
| cout << i << " "; | |
| } | |
| flag = true; | |
| return flag; | |
| } | |
| return flag; | |
| } | |
| //recursion | |
| printSubsetSums(index+1 , slate); | |
| slate.push_back(bag[index]); | |
| printSubsetSums(index+1, slate); | |
| slate.pop_back(); | |
| } | |
| int main(){ | |
| cout<< "Subset sums"<<endl; | |
| bagSize = bag.size(); | |
| vector<int> slate ; | |
| printSubsetSums(index, slate) ; | |
| cout << std::boolalpha << flag; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment