-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDPP37.java
More file actions
71 lines (63 loc) · 1.67 KB
/
DPP37.java
File metadata and controls
71 lines (63 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Google.
The power set of a set is the set of all its subsets. Write a function that, given a set, generates its power set.
For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}.
You may also use a list or array to represent a set.
*/
import java.io.*;
import java.util.*;
class DPP37
{
public static int[] a;
public static void main(String[] ar)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
String[] s = br.readLine().split(" ");
int len = s.length;
a = new int[len];
for(int i=0;i<len;i++)
a[i]=Integer.parseInt(s[i]);
System.out.println(printRes(len));
}
}
public static ArrayList<ArrayList<Integer>> printRes(int len)
{
int[] a1=a;
ArrayList<ArrayList<Integer>> arr1 = new ArrayList<>();
Queue<String> q = new LinkedList<String>();
q.add("1");
int c = (int)Math.pow(2,len)-1;
while(c-->0)
{
String s1 = q.peek(),s2=s1;
q.remove();
arr1.add(addtoArray(s1,len));
s1+="0";
s2+="1";
q.add(s1);
q.add(s2);
}
return arr1;
}
public static ArrayList<Integer> addtoArray(String s,int len)
{
ArrayList<Integer> arr = new ArrayList<>();
int len1 = s.length();
if(len1<len)
{
for(int i=0;i<len-len1;i++)
s="0"+s;
}
System.out.println(s);
for(int i=0;i<len;i++)
{
if(Character.getNumericValue(s.charAt(i))==1)
arr.add(a[i]);
}
System.out.println(arr);
return arr;
}
}