-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10972.java
42 lines (30 loc) · 864 Bytes
/
10972.java
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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
int i;
for(i=arr.length-1; i-1 >= 0 && arr[i-1] > arr[i]; i--);
if (i-1 < 0) {
System.out.println(-1);
System.exit(0);
}
int j = i;
while(j < arr.length-1 && arr[i-1] < arr[j+1] && arr[j] > arr[j+1])
j++;
int temp = arr[i-1];
arr[i-1] = arr[j];
arr[j] = temp;
for(int k=arr.length-1; i<k; i++, k--) {
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
for(int a : arr)
System.out.print(a + " ");
}
}