- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10972.java
More file actions
Latest commit
42 lines (30 loc) · 864 Bytes
/
Copy path10972.java
File metadata and controls
42 lines (30 loc) · 864 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
32
33
34
35
36
37
38
39
40
41
42
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
int[] arr = newint[n];
for(inti=0; i<n; i++) {
arr[i] = sc.nextInt();
}
inti;
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);
}
intj = i;
while(j < arr.length-1 && arr[i-1] < arr[j+1] && arr[j] > arr[j+1])
j++;
inttemp = arr[i-1];
arr[i-1] = arr[j];
arr[j] = temp;
for(intk=arr.length-1; i<k; i++, k--) {
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
for(inta : arr)
System.out.print(a + " ");
}
}