Permalink
Newer
100644
24 lines (19 sloc)
690 Bytes
1
function sortedSquaredArray(array) {
2
// Write your code here.
3
let result = new Array(array.length).fill(0)
4
let left = 0
5
let right = array.length - 1
6
7
for(let index = array.length -1; index >= 0; index--){
8
const leftValue = array[left]
9
const rightValue = array[right]
10
11
if(Math.abs(leftValue) > Math.abs(rightValue)){
12
result[index] = leftValue * leftValue
13
left += 1
14
} else {
15
result[index] = rightValue * rightValue
16
right -= 1
17
}
18
}
19
20
return result;
21
}
22
23
24
console.log('=========>', sortedSquaredArray([1, 2, 3, 5, 6, 8, 9]))