Skip to content

Commit

Permalink
Code for generation of subsets using recursion and backtracking
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitGupta121 committed Oct 17, 2021
1 parent 50ac396 commit 2c8c10a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Generation_of_Subsets_using_Recursion_and_Backtracking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
#include<stdio.h>
using namespace std;

vector<vector<int> > allSubsets;

void generate(vector<int> &subset, int i, vector<int> &nums){
if(i == nums.size()){
allSubsets.push_back(subset);
return;
}

// ith element not in subset
generate(subset, i+1, nums);

// ith element in subset
subset.push_back(nums[i]);
generate(subset, i+1, nums);
subset.pop_back();
}

int main(){
int n;
cin >> n;
vector<int> nums(n);
for(int i = 0 ; i < n; ++i){
cin >> nums[i];
}
vector<int> empty;
generate(empty, 0, nums);
for(auto subset : subsets){
for(auto ele : subset){
cout << ele << " ";
}
cout << endl;
}
}

0 comments on commit 2c8c10a

Please sign in to comment.