Skip to content

Commit 4de4fe7

Browse files
authored
Update PowerSet.java
1 parent 1029a36 commit 4de4fe7

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

Bit Manipulation/PowerSet.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1-
// Question
2-
// Generate Power set using Bitwise operator
1+
/* Question
2+
Generate Power set using Bitwise operator
33
4-
// Sample Case
5-
// Input="ab"
6-
// Output="","a","b","ab"
4+
Sample Case
5+
Input="ab"
6+
Output="","a","b","ab" */
77

88
package javaquestion;
99

1010
import java.util.*;
1111

1212
public class power {
1313
static void subset(String s) {
14+
//To find the length of string
1415
int a = s.length();
16+
17+
// To get total number of subsets
1518
int pow = (int) Math.pow(2, a);
19+
20+
//Outer loop from 0 to pow-1
1621
for (int counter = 0; counter < pow; counter++) {
22+
23+
//inner loop for length of string
1724
for (int j = 0; j < a; j++) {
25+
26+
//To check if nth bit is set or not
1827
if ((counter & (1 << j)) != 0) {
28+
29+
//To print that set bit from string
1930
System.out.print(s.charAt(j));
31+
2032
}
2133
}
34+
//will print line to separate subsets
2235
System.out.println();
2336
}
2437
}
@@ -29,3 +42,5 @@ public static void main(String[] args) {
2942
subset(s);
3043
}
3144
}
45+
46+
// Time complexity- Theta(2^n*n)

0 commit comments

Comments
 (0)