forked from ynyeh0221/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Almost Sorted.js
50 lines (47 loc) · 1.18 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
function processData(input) {
//Enter your code here
input=input.split("\n");
input[1]=input[1].split(" ");
var num1=[];
var num2=[];
for(i=0;i<input[1].length;i++)
{
num1.push(parseInt(input[1][i]));
num2.push(parseInt(input[1][i]));
}
num2.sort(function(a,b){return a-b;});
var diff=[];
for(i=0;i<num1.length;i++)
diff.push(num1[i]-num2[i]);
var check=[];
for(i=0;i<diff.length;i++)
{
if(diff[i]!=0)
check.push([i,diff[i]]);
}
var yes=0;
for(i=0;i<Math.floor(check.length/2);i++)
{
if(check[i][1]==(-1)*check[check.length-1-i][1])
yes=1;
}
if(yes==1)
{
console.log("yes");
if(check.length==2)
console.log("swap "+(check[0][0]+1)+" "+(check[check.length-1][0]+1));
else
console.log("reverse "+(check[0][0]+1)+" "+(check[check.length-1][0]+1));
}
else
console.log("no");
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});