Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created September 3, 2021 06:45
Show Gist options
  • Save misterpoloy/ed31bbf323fb89b727dd098f9cd3cd34 to your computer and use it in GitHub Desktop.
Save misterpoloy/ed31bbf323fb89b727dd098f9cd3cd34 to your computer and use it in GitHub Desktop.
Sorted Square Array
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;
@misterpoloy
Copy link
Author

screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment