-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerSet.java
More file actions
38 lines (31 loc) · 1.12 KB
/
PowerSet.java
File metadata and controls
38 lines (31 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
/* *****************************************************************************
* Name: Jaya Mukherjee
* Coursera User ID: xxxx
* Last modified: October 8, 2021
**************************************************************************** */
public class PowerSet {
static void printPowerSet(char[] set,
int set_size) {
/* set_size of power set of a set
with set_size n is (2**n -1) */
long pow_set_size =
(long) Math.pow(2, set_size);
int counter, j;
// Run from counter 000..0 to 111..1
for (counter = 0; counter <
pow_set_size; counter++) {
for (j = 0; j < set_size; j++) {
/* Check if jth bit in the
counter is set If set then
print jth element from set */
if ((counter & (1 << j)) > 0)
System.out.print(set[j]);
}
System.out.println();
}
}
public static void main(String[] args) {
char[] set = { 'a', 'b', 'c' };
printPowerSet(set, 3);
}
}