-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10973.java
More file actions
44 lines (37 loc) · 1.15 KB
/
10973.java
File metadata and controls
44 lines (37 loc) · 1.15 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
//import java.io.*;
//import java.util.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N;
static int[] nums;
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
nums = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
if (nextPermutation()) {
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
} else {
System.out.println("-1");
}
}
private static boolean nextPermutation() {
int i = nums.length - 1;
while (i > 0 && nums[i-1] <= nums[i]) { i--; }
if (i <= 0) return false;
int j = nums.length - 1;
while (nums[j] >= nums[i - 1]) { j--; }
swap(i - 1, j);
j = nums.length - 1;
while (i < j) {
swap(i, j);
i++; j--;
}
return true;
}
private static void swap(int idx1, int idx2) {
int temp = nums[idx1];
nums[idx1] = nums[idx2];
nums[idx2] = temp;
}
}