Last active
November 26, 2020 15:24
-
-
Save graphoarty/3c310b2491dedfc41ccebe4ddff3ead0 to your computer and use it in GitHub Desktop.
Generate Subsets in C++
This file contains 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; | |
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