code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

977 Squares Of A Sorted Array

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * Given an array of integers A sorted in non-decreasing order,
 * return an array of the squares of each number, also in sorted
 * non-decreasing order.
 *
 * Example 1:
 * Input: [-4,-1,0,3,10]
 * Output: [0,1,9,16,100]
 */

function squaresOfASortedArray(nums) {
  let length = nums.length,
    left = 0,
    right = length - 1,
    index = length - 1,
    result = new Array(nums.length).fill(0)
  while (left <= right) {
    const leftElement = nums[left] * nums[left]
    const rightElement = nums[right] * nums[right]
    if (leftElement > rightElement) {
      result[index] = leftElement
      left++
    } else {
      result[index] = rightElement
      right--
    }
    index--
  }
  return result
}

test('squares Of A Sorted Array', () => {
  expect(squaresOfASortedArray([-4, -1, 0, 3, 10])).toEqual([0, 1, 9, 16, 100])
})

Created 2020-04-21T11:47:36+00:00 · Edit