-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSubsets.cpp
50 lines (39 loc) · 1.04 KB
/
Subsets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
//recursive approach to find the subsets
void find(vector<int> nums, int index, vector<int> ans, vector<vector<int>>& result){
if(index>=nums.size()){
result.push_back(ans);
return;
}
//exclude the element
find(nums,index+1,ans,result);
//include the element
ans.push_back(nums[index]);
find(nums,index+1,ans,result);
}
void subsets(vector<int>& nums) {
vector<vector<int>> result;
vector<int>ans;
find(nums,0,ans,result);
print(result);
return;
}
//printing the subsets of the input vector
void print(vector<vector<int>> vec){
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++)
cout << vec[i][j] << " ";
cout << endl;
}
}
};
int main()
{
Solution obj;
vector<int> nums = {1,2,3};
obj.subsets(nums);
return 0;
}