-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBAEKJOON_10972
More file actions
59 lines (49 loc) · 1.4 KB
/
BAEKJOON_10972
File metadata and controls
59 lines (49 loc) · 1.4 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.*;
import java.util.*;
public class Main {
//1 3 2 4
public static boolean next_permutation(int[] arr){
int i = arr.length-1;
while(i>0 && arr[i-1] >= arr[i]){
i-=1;
}
if(i<=0){
return false;
}
int j=arr.length-1;
while(arr[j] <=arr[i-1]){
j-=1;
}
int temp = arr[i-1];
arr[i-1] = arr[j];
arr[j] = temp;
j=arr.length-1;
while(i<j){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i+=1;
j-=1;
}
return true;
}
public static void main(String[] args) throws IOException{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine()); //N은 1~N 까지의 자연수 4 - 1,2,3,4
int[] arr = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++){
arr[i]=Integer.parseInt(st.nextToken());
}
//permutation
if(next_permutation(arr)){
for(int i=0; i<N; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}else{
System.out.println("-1");
}
}
}