-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestPermutation.java
More file actions
31 lines (30 loc) · 848 Bytes
/
LargestPermutation.java
File metadata and controls
31 lines (30 loc) · 848 Bytes
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
import java.io.*;
import java.util.*;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
int arr[] = new int[n];
int pos[] = new int[n+1];
for(int i = 0; i < n ; i++){
arr[i] = sc.nextInt();
pos[arr[i]] = i;
}
for(int i = 0; i < n && k>0; i++){
if(arr[i] == n-i)
continue;
int t1 = pos[n-i];
int t2 = pos[arr[i]];
pos[n-i] = pos[arr[i]];
pos[arr[i]] = t1;
int t3 = arr[t1];
arr[t1] = arr[t2];
arr[t2] = t3;
k--;
}
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}