- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsetOfSet.java
More file actions
Latest commit
42 lines (36 loc) · 944 Bytes
/
Copy pathsubsetOfSet.java
File metadata and controls
42 lines (36 loc) · 944 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
35
36
37
38
39
40
41
42
packageBit_Manipulation;
importjava.util.Scanner;
/*
* Generate possible Subsets for given set.
*
* */
publicclasssubsetOfSet {
publicstaticvoidmain(String... args) {
Scannersc = newScanner(System.in);
// char ch[]={'a','b','c','d'};
System.out.print("How many element in set? ");
intn = sc.nextInt();
char[] ch = newchar[n];
System.out.print("Enter the char elements: ");
for (intk = 0; k < n; k++) {
charc = sc.next().charAt(0);
ch[k] = c;
}
subsetOfSet.possibleSubset(ch, n);
sc.close();
}
staticvoidpossibleSubset(char[] c, intn) {
for (inti = 0; i < (1 << n); ++i) {// here i loop until it reaches
// 2powN (1<<n). bcz
// there are 2powN subset for a set
// with N element.
System.out.print("{");
for (intj = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
System.out.print(c[j] + " ");
}
}
System.out.print("}, ");
}
}
}