-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortTheArray.java
58 lines (50 loc) · 1.37 KB
/
SortTheArray.java
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
/* https://codeforces.com/contest/451/problem/B
#sorting #implementation
sort the original array (A) to having the sorted array (B)
find out the segment in A that differ the corresponding segment in B.
and revert the segment, compare reverted segment with the corresponding segment in B.
*/
import java.util.Arrays;
import java.util.Scanner;
public class SortTheArray {
static void canorgArrayWithRevertingOneSegment() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] orgArr = new int[n];
int[] sortedArr = new int[n];
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
orgArr[i] = temp;
sortedArr[i] = temp;
}
Arrays.sort(sortedArr);
int l = 0;
int r = 0;
boolean startFlg = true;
for (int i = 0; i < n; i++) {
if (orgArr[i] != sortedArr[i]) {
if (startFlg) {
l = i;
startFlg = false;
} else {
r = i;
}
}
}
String resultSegment = "1 1";
if (l != r) {
for (int i = l, j = r; i <= r; i++, j--) {
if (orgArr[i] != sortedArr[j]) {
System.out.println("no");
return;
}
}
resultSegment = (l + 1) + " " + (r + 1);
}
System.out.println("yes");
System.out.println(resultSegment);
}
public static void main(String[] args) {
canorgArrayWithRevertingOneSegment();
}
}