-
Notifications
You must be signed in to change notification settings - Fork 0
/
All Unique Permutations of an array
124 lines (96 loc) · 3.71 KB
/
All Unique Permutations of an array
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
All Unique Permutations of an array
Difficulty: MediumAccuracy: 52.85%Submissions: 40K+Points: 4
Given an array arr[] of length n. Find all possible unique permutations of the array in sorted order. A sequence A is greater than sequence B if there is an index i for which Aj = Bj for all j<i and Ai > Bi.
Example 1:
Input:
n = 3
arr[] = {1, 2, 1}
Output:
1 1 2
1 2 1
2 1 1
Explanation:
These are the only possible unique permutations
for the given array.
Example 2:
Input:
n = 2
arr[] = {4, 5}
Output:
Only possible 2 unique permutations are
4 5
5 4
Your Task:
You don't need to read input or print anything. You only need to complete the function uniquePerms() that takes an integer n, and an array arr of size n as input and returns a sorted list of lists containing all unique permutations of the array.
Expected Time Complexity: O(n*n!)
Expected Auxilliary Space: O(n*n!)
Constraints:
1 ≤ n ≤ 9
solution:
//{ Driver Code Starts
//Initial Template for JAVA
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int n = Integer.parseInt(read.readLine());
String S[] = read.readLine().split(" ");
ArrayList<Integer> arr = new ArrayList<>();
for(int i=0; i<n; i++)
arr.add(Integer.parseInt(S[i]));
Solution ob = new Solution();
ArrayList<ArrayList<Integer>> res = ob.uniquePerms(arr,n);
for(int i=0; i<res.size(); i++)
{
for(int j=0; j<n; j++)
{
System.out.print(res.get(i).get(j) + " ");
}
System.out.println();
}
}
}
}
// } Driver Code Ends
//User function Template for Java
//User function Template for Java
class Solution {
static ArrayList<ArrayList<Integer>> uniquePerms(ArrayList<Integer> arr, int n) {
// Sort the array to handle duplicates easily
Collections.sort(arr);
// This will store all unique permutations
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
// Set to keep track of visited elements
ArrayList<Integer> currentPerm = new ArrayList<>();
boolean[] visited = new boolean[n];
// Start the backtracking process
generatePermutations(arr, result, currentPerm, visited);
return result;
}
private static void generatePermutations(ArrayList<Integer> arr, ArrayList<ArrayList<Integer>> result,
ArrayList<Integer> currentPerm, boolean[] visited) {
// If the current permutation is of the same length as the array, add to result
if (currentPerm.size() == arr.size()) {
result.add(new ArrayList<>(currentPerm));
return;
}
for (int i = 0; i < arr.size(); i++) {
// Skip if the element is already visited or if it's a duplicate and the previous duplicate is not visited
if (visited[i] || (i > 0 && arr.get(i).equals(arr.get(i - 1)) && !visited[i - 1])) {
continue;
}
// Mark the element as visited
visited[i] = true;
currentPerm.add(arr.get(i));
// Recur to build the permutation
generatePermutations(arr, result, currentPerm, visited);
// Backtrack
visited[i] = false;
currentPerm.remove(currentPerm.size() - 1);
}
}
}