-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthPermutationArray.java
More file actions
66 lines (57 loc) · 2.29 KB
/
KthPermutationArray.java
File metadata and controls
66 lines (57 loc) · 2.29 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
import java.util.*;
public class KthPermutationArray {
public static List<String> getAllPermutations(int[] arr) {
List<String> result = new ArrayList<>();
Arrays.sort(arr); // Ensure permutations are in lexicographical order
permute(arr, new boolean[arr.length], new ArrayList<>(), result);
return result;
}
private static void permute(int[] arr, boolean[] used, List<Integer> current, List<String> result) {
if (current.size() == arr.length) {
result.add(current.toString().replaceAll("[\\[\\],]", ""));
return;
}
for (int i = 0; i < arr.length; i++) {
if (!used[i]) {
used[i] = true;
current.add(arr[i]);
permute(arr, used, current, result);
used[i] = false;
current.remove(current.size() - 1);
}
}
}
public static String getPermutation(int[] arr, int k) {
List<String> allPermutations = getAllPermutations(arr);
if (k > allPermutations.size()) {
return "Invalid value as there are only " + allPermutations.size() + " possible permutations for this array.";
}
return allPermutations.get(k - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter array size: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter array elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter k (or type 'X' to list all permutations): ");
if (sc.hasNextInt()) {
int k = sc.nextInt();
System.out.println("K-th permutation: " + getPermutation(arr, k));
} else {
String input = sc.next();
if (input.equalsIgnoreCase("X")) {
System.out.println("All possible permutations:");
List<String> allPermutations = getAllPermutations(arr);
for (int i = 0; i < allPermutations.size(); i++) {
System.out.println((i + 1) + ": " + allPermutations.get(i));
}
} else {
System.out.println("Invalid input.");
}
}
}
}