-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlmostSorted.java
More file actions
68 lines (50 loc) · 1.6 KB
/
Copy pathAlmostSorted.java
File metadata and controls
68 lines (50 loc) · 1.6 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
60
61
62
63
64
65
66
67
68
package rank;
import java.util.Scanner;
/**
* created by @author suraj on 13/11/19
*/
public class AlmostSorted {
private static final Scanner scanner = new Scanner(System.in);
static void almostSorted(int[] arr) {
int swapCount = 0;
int swapI = -1;
int swapJ = -1;
int reverseL = -1;
int reverseR = -1;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
reverseR = i + 1;
swapCount += 1;
if (swapCount == 1) {
swapI = i;
reverseL = i;
swapJ = i + 1;
}
}
}
if (swapCount == 0) {
System.out.println("yes");
} else if (swapCount == 1) {
System.out.println("yes");
System.out.println("swap " + (swapI + 1) + " " + (swapJ + 1));
} else if (reverseR != -1) {
System.out.println("yes");
System.out.println("reverse " + (reverseL + 1) + " " + (reverseR + 1));
} else {
System.out.println("no");
}
}
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] arr = new int[n];
String[] arrItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int arrItem = Integer.parseInt(arrItems[i]);
arr[i] = arrItem;
}
almostSorted(arr);
scanner.close();
}
}