👋
Welcome to my blog!

Sorted Squared Array

Explore how to transform a sorted array into an array of squared numbers while maintaining the sorted order.

Sorted Squared Array
array
easy

Published At

6/4/2021

Reading Time

~ 3 min read

Write a function that takes in a non-empty array of integers that are sorted in ascending order and returns a new array of the same length with the squares of the original integers also sorted in ascending order.

Sample Input

js
array = [1, 2, 3, 5, 6, 8, 9]
js
array = [1, 2, 3, 5, 6, 8, 9]

Sample Output

js
;[1, 4, 9, 25, 36, 64, 81]
js
;[1, 4, 9, 25, 36, 64, 81]

Hints

Hint 1

While the integers in the input array are sorted in increasing order, their squares won't necessarily be as well, because of the possible presence of negative numbers.

Hint 2

Traverse the array value by value, square each value, and insert the squares into an output array. Then, sort the output array before returning it. Is this the optimal solution?

Hint 3

To reduce the time complexity of the algorithm mentioned in Hint #2, you need to avoid sorting the ouput array. To do this, as you square the values of the input array, try to directly insert them into their correct position in the output array.

Hint 4

Use two pointers to keep track of the smallest and largest values in the input array. Compare the absolute values of these smallest and largest values, square the larger absolute value, and place the square at the end of the output array, filling it up from right to left. Move the pointers accordingly, and repeat this process until the output array is filled.

Optimal Space & Time Complexity

O(n) time | O(n) space - where n is the length of the input array

Solution-1
js
function sortedSquaredArray(array) {
  let squared = []
  for (let num of array) {
    squared.push(num * num)
  }
  return squared.sort((a, b) => a - b)
}
Solution-1
js
function sortedSquaredArray(array) {
  let squared = []
  for (let num of array) {
    squared.push(num * num)
  }
  return squared.sort((a, b) => a - b)
}
Solution-2
js
function sortedSquaredArray(array) {
  const sorted = new Array(array.length - 1).fill(0)
  let smallerIndex = 0
  let largerIndex = array.length - 1
 
  for (let index = array.length - 1; index >= 0; index--) {
    const smallerValue = array[smallerIndex]
    const largerValue = array[largerIndex]
 
    if (Math.abs(smallerValue) > Math.abs(largerValue)) {
      sorted[index] = smallerValue * smallerValue
      smallerIndex++
    } else {
      sorted[index] = largerValue * largerValue
      largerIndex--
    }
  }
  return sorted
}
Solution-2
js
function sortedSquaredArray(array) {
  const sorted = new Array(array.length - 1).fill(0)
  let smallerIndex = 0
  let largerIndex = array.length - 1
 
  for (let index = array.length - 1; index >= 0; index--) {
    const smallerValue = array[smallerIndex]
    const largerValue = array[largerIndex]
 
    if (Math.abs(smallerValue) > Math.abs(largerValue)) {
      sorted[index] = smallerValue * smallerValue
      smallerIndex++
    } else {
      sorted[index] = largerValue * largerValue
      largerIndex--
    }
  }
  return sorted
}
Solution-3
js
function sortedSquaredArray(array) {
  let sorted = new Array(array.length).fill(0)
  let smallerIndex = 0
  let largerIndex = array.length - 1
 
  for (let i = array.length - 1; i >= 0; i--) {
    let smallerValue = array[smallerIndex]
    let largerValue = array[largerIndex]
 
    if (Math.abs(smallerValue) > Math.abs(largerValue)) {
      sorted[i] = smallerValue * smallerValue
      smallerIndex++
    } else {
      sorted[i] = largerValue * largerValue
      largerIndex--
    }
  }
  return sorted
}
Solution-3
js
function sortedSquaredArray(array) {
  let sorted = new Array(array.length).fill(0)
  let smallerIndex = 0
  let largerIndex = array.length - 1
 
  for (let i = array.length - 1; i >= 0; i--) {
    let smallerValue = array[smallerIndex]
    let largerValue = array[largerIndex]
 
    if (Math.abs(smallerValue) > Math.abs(largerValue)) {
      sorted[i] = smallerValue * smallerValue
      smallerIndex++
    } else {
      sorted[i] = largerValue * largerValue
      largerIndex--
    }
  }
  return sorted
}

🌳

Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.

Have a wonderful day.
Abhishek 🙏

Join My Exclusive Newsletter Community

Step into a world where creativity intersects with technology. By subscribing, you'll get a front-row seat to my latest musings, full-stack development resources, and exclusive previews of future posts. Each email is a crafted experience that includes:

  • In-depth looks at my covert projects and musings to ignite your imagination.
  • Handpicked frontend development resources and current explorations, aimed at expanding your developer toolkit.
  • A monthly infusion of inspiration with my personal selection of quotes, books, and music.

Embrace the confluence of words and wonder, curated thoughtfully and sent straight to your inbox.

No fluff. Just the highest caliber of ideas.