Skip to content

Commit 8788e26

Browse files
Merge pull request #298 from 2devyank/master
Power set
2 parents b30cc1c + 4de4fe7 commit 8788e26

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Bit Manipulation/PowerSet.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
//To find the length of string
15+
int a = s.length();
16+
17+
// To get total number of subsets
18+
int pow = (int) Math.pow(2, a);
19+
20+
//Outer loop from 0 to pow-1
21+
for (int counter = 0; counter < pow; counter++) {
22+
23+
//inner loop for length of string
24+
for (int j = 0; j < a; j++) {
25+
26+
//To check if nth bit is set or not
27+
if ((counter & (1 << j)) != 0) {
28+
29+
//To print that set bit from string
30+
System.out.print(s.charAt(j));
31+
32+
}
33+
}
34+
//will print line to separate subsets
35+
System.out.println();
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
Scanner sc = new Scanner(System.in);
41+
String s = sc.next();
42+
subset(s);
43+
}
44+
}
45+
46+
// Time complexity- Theta(2^n*n)

0 commit comments

Comments
 (0)