Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
598 B
3
Indexable
Never

var squareSortedArray = function(nums) {
    let firstPointer = 0;
    let lastPointer = nums.length -1;
    let index = nums.length -1;
    let result = [];
    
    while(firstPointer <= lastPointer) {
        if (Math.abs(nums[firstPointer]) < Math.abs(nums[lastPointer])) {
            result[index--] = nums[lastPointer] * nums[lastPointer];
            lastPointer--;
        } else {
            result[index--] = nums[firstPointer] * nums[firstPointer];
            firstPointer++;
        }
    }
    
    return result;
}

let nums = [-4,-1,0,3,10];
console.log(squareSortedArray(nums));