Created
September 3, 2021 06:45
-
-
Save misterpoloy/ed31bbf323fb89b727dd098f9cd3cd34 to your computer and use it in GitHub Desktop.
Sorted Square Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortedSquaredArray(array) { | |
// Write your code here. | |
const squareResult = new Array(array.length) | |
let leftPointer = 0 | |
let rightPointer = array.length - 1 | |
// The important part is to reverse the array | |
for (let i = rightPointer; i >= 0; i--) { | |
let leftValue = Math.abs(Math.pow(array[leftPointer], 2)) | |
let rightValue = Math.abs(Math.pow(array[rightPointer], 2)) | |
squareResult[i] = Math.max(leftValue, rightValue) | |
if (leftValue > rightValue) { | |
leftPointer++; | |
} else { | |
rightPointer--; | |
} | |
} | |
return squareResult; | |
} | |
// Do not edit the line below. | |
exports.sortedSquaredArray = sortedSquaredArray; |
Author
misterpoloy
commented
Sep 3, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment