-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalmostSorted.js
47 lines (44 loc) · 1.17 KB
/
almostSorted.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
/*
* Complete the 'almostSorted' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
function almostSorted(arr) {
// Write your code here
let arrSorted = [];
let resTab = [];
let isConsecutive = true;
for(let i=0; i<arr.length; i++){
arrSorted[i] = arr[i];
}
arr.sort((a, b) => a-b);
console.log(arr);
console.log(arrSorted);
for(let i=0; i<arrSorted.length; i++){
if(arr[i] != arrSorted[i]){
resTab.push(i);
}
}
if(resTab.length == 2){
console.log('yes');
console.log('swap'+' '+resTab[0]+' '+resTab[1]);
}else if(resTab.length == 0){
console.log('yes');
}else if(resTab.length > 2){
for(let i=1; i<resTab.length; i++){
if(resTab[i] != resTab[i-1]+1){
isConsecutive = false;
break;
}
}
if(isConsecutive == false){
console.log('no');
}else{
console.log('yes');
console.log('reverse'+' '+resTab[0]+' '+resTab[resTab.length-1]);
}
}
}
let tab = [3, 5, 2, 1];
let res = tab.sort((a, b) => a-b);
console.log('res', res);