-
Notifications
You must be signed in to change notification settings - Fork 1
/
powerSet.cpp
46 lines (42 loc) · 1.09 KB
/
powerSet.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
/*****************************************************************************
* PowerSet class prints the power set of a given array
* Ex: If the input array is [2 3 4] then the power set is
* [], [2], [3], [4], [2, 3], [2, 4], [3, 4], [2, 3, 4]
* Given an array of length N, there are 2^N elements in a power set
* (because every element has 2 choices, either it is present or not)
*
*****************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
class PowerSet {
public:
void printPowerSet(vector<int> &a, int length)
{
int bitMask = 0;
int numPowerSet = 1 << length;
for (int i = 0; i < numPowerSet; i++) {
cout << "[";
bitMask = i;
for (int j = 0; j < length; j++) {
if (!(bitMask >> j))
break; /* Stop here we don't have to go till length */
if ((bitMask >> j) & 0x1) {
cout << a[j] << ",";
}
}
cout << "]" << endl;
}
}
};
#define LEN 3
int main()
{
vector <int> a;
for (int i = 0; i < LEN; i++) {
a.push_back(i+3);
}
PowerSet p;
p.printPowerSet(a, LEN);
return 0;
}