Skip to content

Commit 1029a36

Browse files
authored
Update PowerSet.java
1 parent 18cc865 commit 1029a36

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Bit Manipulation/PowerSet.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Question
2+
// Generate Power set using Bitwise operator
3+
4+
// Sample Case
5+
// Input="ab"
6+
// Output="","a","b","ab"
7+
8+
package javaquestion;
9+
10+
import java.util.*;
11+
12+
public class power {
13+
static void subset(String s) {
14+
int a = s.length();
15+
int pow = (int) Math.pow(2, a);
16+
for (int counter = 0; counter < pow; counter++) {
17+
for (int j = 0; j < a; j++) {
18+
if ((counter & (1 << j)) != 0) {
19+
System.out.print(s.charAt(j));
20+
}
21+
}
22+
System.out.println();
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
Scanner sc = new Scanner(System.in);
28+
String s = sc.next();
29+
subset(s);
30+
}
31+
}

0 commit comments

Comments
 (0)