Skip to content

Commit

Permalink
Create 0977-squares-of-a-sorted-array.js
Browse files Browse the repository at this point in the history
  • Loading branch information
andynullwong committed Jan 2, 2023
1 parent e2aab2b commit b393feb
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions javascript/0977-squares-of-a-sorted-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortedSquares = function (nums) {
let left = 0;
let right = nums.length - 1;

const answer = [];

while (left <= right) {
const leftSqr = Math.pow(nums[left], 2);
const rightSqr = Math.pow(nums[right], 2);

if (leftSqr > rightSqr) {
answer.push(leftSqr);
left++;
} else {
answer.push(rightSqr);
right--;
}
}
return answer.reverse();
};

0 comments on commit b393feb

Please sign in to comment.