-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutations.java
More file actions
48 lines (37 loc) · 878 Bytes
/
Permutations.java
File metadata and controls
48 lines (37 loc) · 878 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
43
44
45
46
47
48
import java.util.*;
public class Solution {
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();
while(true){
int i = n - 1;
while(i > 0 && arr[i - 1] > arr[i])
i--;
if(i <= 0)
break;
int j = n - 1;
while(j > (i - 1) && arr[i - 1] > arr[j])
j--;
if(j == i-1)
break;
int temp = arr[j];
arr[j] = arr[i - 1];
arr[i - 1] = temp;
for(int k = i; k < n - 1; k++){
if(arr[k] > arr[k+1]){
temp = arr[k+1];
arr[k+1] = arr[k];
arr[k] = temp;
}
}
for(int k = 0; k < n; k++)
System.out.print(arr[k]+" ");
System.out.println();
// This is only for testing purposes! okay!!
//git is sooo cooool!!!!!
}
}
}