-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicographicallySmallestArray.java
More file actions
37 lines (28 loc) · 1.07 KB
/
LexicographicallySmallestArray.java
File metadata and controls
37 lines (28 loc) · 1.07 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
import java.util.*;
public class LexicographicallySmallestArray {
public static int[] lexicographicallySmallestArray(int[] arr, int n, int K) {
int remainingSwaps = K;
for (int i = 0; i < n - 1 && remainingSwaps > 0; i++) {
int minIndex = i;
for (int j = i + 1; j < Math.min(i + remainingSwaps + 1, n); j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
while (minIndex > i && remainingSwaps > 0) {
int temp = arr[minIndex];
arr[minIndex] = arr[minIndex - 1];
arr[minIndex - 1] = temp;
minIndex--;
remainingSwaps--;
}
}
return arr;
}
public static void main(String[] args) {
int[] arr = {7, 6, 9, 2, 1};
int K = 3;
int[] result = lexicographicallySmallestArray(arr, arr.length, K);
System.out.println("Lexicographically smallest array: " + Arrays.toString(result));
}
}