-
Notifications
You must be signed in to change notification settings - Fork 2
/
almost-sorted.js
90 lines (76 loc) · 2.08 KB
/
almost-sorted.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function check(arr) {
for (let i = 0; i <= arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) return false
}
return true
}
function almostSorted(arr) {
// Complete this function
let ar = [-1, ...arr, 1000001];
let peaks = [];
let valleys = [];
let peak;
let valley;
for (let i = 1; i < ar.length - 1; i++) {
if (ar[i] > ar[i - 1] && ar[i] > ar[i + 1]) {
peaks.push(i);
}
if (ar[i] < ar[i - 1] && ar[i] < ar[i + 1]) {
valleys.push(i);
}
}
if (peaks.length == 0 && valleys.length == 0) {
return console.log('yes')
} else if (peaks.length == 1 && valleys.length == 1) {
peak = peaks[0];
valley = valleys[0];
} else if (peaks.length == 2 && valleys.length == 2) {
if (peaks[0] + 1 == valleys[0]) {
peak = peaks[0];
valley = valleys[1];
} else {
return console.log('no')
}
} else {
return console.log('no')
}
if (peak > valley) {
return console.log('no')
}
let swapAr = [...ar];
swapAr[peak] = ar[valley];
swapAr[valley] = ar[peak];
if (check(swapAr)) {
return console.log(`yes\nswap ${peak} ${valley}`)
}
let reverseAr = [...ar];
for (let i = peak; i <= valley; i++) {
reverseAr[i] = ar[valley + peak - i];
}
if (check(reverseAr)) {
return console.log(`yes\nreverse ${peak} ${valley}`)
}
console.log('no');
}
function main() {
var n = parseInt(readLine());
arr = readLine().split(' ');
arr = arr.map(Number);
almostSorted(arr);
}