-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpower set .... subsets of set.cpp
84 lines (57 loc) Β· 1.45 KB
/
power set .... subsets of set.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
///////////////////////////////////////////
find and print subsets of a given set using bitwise operator
///////////////////////////////////////////
*/
#include <bits/stdc++.h>
#define int long long int
#define setbits(x) __builtin_popcount(x)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define ct(x) cout<<x<<endl;
#define ct2(x,y) cout<<x<<" "<<y<<endl;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define forx(i, x, n) for (int i = x ; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
//typedef long long int lli;
typedef long double ld;
//typedef pair<int, int> pii;
//typedef std::vector<int> vi;
//typedef vector<pii> vii;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int32_t main() {
///////////
c_p_c();
///////////
// code
// let us put the inputs in the array
int n ; // inputs ;
cin >> n ;
int arr[n] ;
forn(i, n) {
cin >> arr[i] ;
}
// now find all the subsets in the array using bitwise operator
// total number of subsets for a set having n elements is 2^n - 1
// Also 1<<2 = 1 * 2^2
// wherever we get a bit , we print that element from the array
forn(i , (1 << n)) {
forn(j, n ) {
if ( i & (1 << j)) {
cout << (arr[j]) << " ";
}
}
cout << endl ;
}
return 0;
}