-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path130.unique_subsets.java
More file actions
64 lines (56 loc) · 1.69 KB
/
Copy path130.unique_subsets.java
File metadata and controls
64 lines (56 loc) · 1.69 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
// Unique Subsets
// https://practice.geeksforgeeks.org/problems/subsets-1587115621/1
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for(int t=0;t<testCases;t++){
int n = sc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
System.out.print("()");
ArrayList <ArrayList<Integer>> res = new solve().AllSubsets(arr,n);
for (int i = 0; i < res.size (); i++)
{
System.out.print ("(");
for (int j = 0; j < res.get(i).size (); j++)
{
if (j != res.get(i).size()-1)
System.out.print ((res.get(i)).get(j) + " ");
else
System.out.print ((res.get(i)).get(j));
}
System.out.print (")");
}
System.out.println();
}
}
}// } Driver Code Ends
class solve
{
//Function to find all possible unique subsets.
public static ArrayList <ArrayList <Integer>> AllSubsets(int arr[], int n)
{
Arrays.sort(arr);
ArrayList<ArrayList<Integer>> ans=new ArrayList<>();
getSet(arr,new ArrayList<Integer>(),n,ans,0);
ans.remove(0);
return ans;
}
public static void getSet(int []arr,ArrayList<Integer> ls,int n,ArrayList<ArrayList<Integer>> ans,
int ind){
ans.add(new ArrayList<>(ls));
for(int i=ind;i<n;i++){
if(i!=ind&&arr[i]==arr[i-1]) continue;
ls.add(arr[i]);
getSet(arr,ls,n,ans,i+1);
ls.remove(ls.size()-1);
}
}
}