Skip to content

Instantly share code, notes, and snippets.

@MohammedALREAI
Created April 5, 2021 23:44
Show Gist options
  • Save MohammedALREAI/dae83528a0018062c4871ca8aaa095a2 to your computer and use it in GitHub Desktop.
Save MohammedALREAI/dae83528a0018062c4871ca8aaa095a2 to your computer and use it in GitHub Desktop.
977. Squares of a Sorted Array
function sortedSquares(nums: number[]=[-4,-5,0,1,2]): number[] {
let left =0
let right=nums.length-1;
let arr= new Array(nums.length).fill(0)
for(let i=0;i<nums.length;i++){
if(Math.abs (nums[left])>Math.abs (nums[right])){
arr[i]=nums[left]*nums[left] ;
left++
}
else{
arr[i]=nums[right]*nums[right]
right--
}
}
return arr.reverse()
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment