Skip to content

Instantly share code, notes, and snippets.

@graphoarty
Last active November 26, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphoarty/3c310b2491dedfc41ccebe4ddff3ead0 to your computer and use it in GitHub Desktop.
Save graphoarty/3c310b2491dedfc41ccebe4ddff3ead0 to your computer and use it in GitHub Desktop.
Generate Subsets in C++
#include<iostream>
#include<vector>
using namespace std;
int print_vector(vector<int> arr){
for(int i = 0; i < (int) arr.size(); i++){
cout << arr[i] << " ";
}
cout << "\n";
return 0;
}
int generate_subsets(vector<int> arr, int n, int k){
if(k > n){
print_vector(arr);
return 0;
} else {
arr.push_back(k);
generate_subsets(arr, n, k + 1);
arr.pop_back();
generate_subsets(arr, n, k + 1);
}
return 0;
}
int main(int argc, char const *argv[])
{
vector<int> arr;
generate_subsets(arr, 3, 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment