-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.cpp
More file actions
60 lines (45 loc) · 1.12 KB
/
Copy pathpractice.cpp
File metadata and controls
60 lines (45 loc) · 1.12 KB
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
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
vector<int> currSubset(int num, vector<int>& arr){
vector<int> bits;
int j = 0;
while(num > 0){
if((num & 1) == 1){
bits.push_back(arr[j]);
}
num /= 2;
j++;
}
return bits;
}
void generateAllSubsets(int n , vector<int>& arr){
int options = (1 << n );
int st = 0;
int en = options - 1;
vector< vector< int > > ans;
for(int i = st ; i <= en ; i++){
vector<int> currSet = currSubset(i,arr);
ans.push_back(currSet);
}
cout<<" num of sets ... "<<ans.size()<<endl;
for(int i = 0 ; i < ans.size(); i++){
cout<<" set "<<(i + 1)<<endl;
cout<<" ------------------------- "<<endl;
for(int j = 0 ; j < ans[i].size(); j++){
cout<<ans[i][j] << " ";
}
cout<<endl<<endl;
cout<<" ------------------------- "<<endl;
}
}
int main(){
// generate all subsets using bit manupulation ...
int n = 3;
//vector<int> arr = {1,3,4,5,6};
vector<int> arr ;
arr.push_back(1);
arr.push_back(3);
arr.push_back(4);
generateAllSubsets(n,arr);
return 0;
}