File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 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)
You can’t perform that action at this time.
0 commit comments