File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
88package javaquestion ;
99
1010import java .util .*;
1111
1212public 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)
You can’t perform that action at this time.
0 commit comments