-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathpowerSet.java
More file actions
34 lines (29 loc) · 803 Bytes
/
powerSet.java
File metadata and controls
34 lines (29 loc) · 803 Bytes
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
import java.util.*;
public class powerSet{
public static void main(String[] args) {
String[] input = {"", "", ""};
if(args.length > 0){
for(int i = 0; i < args.length; i++){
input[i] = args[i];
}
}
LinkedHashSet hashSet = new LinkedHashSet();
int len = input.length;
int elements = (int) Math.pow(2, len);
for (int i = 0; i < elements; i++) {
String string = Integer.toBinaryString(i);
int value = string.length();
String pset = string;
for (int k = value; k < len; k++) {
pset = "0" + pset;
}
LinkedHashSet set = new LinkedHashSet();
for (int j = 0; j < pset.length(); j++) {
if (pset.charAt(j) == '1')
set.add(input[j]);
}
hashSet.add(set);
}
System.out.println(hashSet);
}
}