Squaring a sorted array

golibrary co
Mar 26 · 3 min read
Squaring a sorted array
Squaring a sorted array

Problem Statement

Given a sorted array, create a new array containing squares of all the number of the input array in the sorted order.

Example 1:

Input:
Output:

Example 2:


Input:

Output:

Solution 1

One simple solution is to loop through the array elements and square them in place and then store the sorted squared array in output squares array and return it.

Javascript code below:-

function make_squares(arr) {
squares =
// TODO: Write your code here
arr = arr.map(item=>item*item);
squares = arr.sort((a,b)=>a-b);
return squares;
};
document.write(`Squares: ${make_squares()}
`);
document.write(`Squares: ${make_squares()}
`);



Time complexity

The time complexity of the above algorithm will be O(N log N) as we are first sorting the array and then iterating the input array only once.

Space complexity

The space complexity of the above algorithm will also be O(N); this space will be used for the output array.

The above solution works but isn’t efficient as we are sorting and then iterating through the array again to store it in squares array. Better solution would be to use a 2 pointer approach and binary search mechanism to insert the squared elements in a sorted way as we iterate through the array.

Solution 2

This question becomes tricky because the array has negative numbers as well. An easier approach could be to first find the index of the first non-negative number in the array. After that, we can use Two Pointers to iterate the array. One pointer will move forward to iterate the non-negative numbers and the other pointer will move backward to iterate the negative numbers. At any step, whichever number gives us a bigger square will be added to the output array.

For the above-mentioned Example-1, we will do something like this:

Squared Sorted Array
Squared Sorted Array

Squared Sorted Array1



Since the numbers at both the ends can give us the largest square, an alternate approach could be to use two pointers starting at both the ends of the input array to iterate faster. At any step, whichever pointer gives us the bigger square we add it to the result array and move to the next/previous number according to the pointer. For the above-mentioned Example-1, we will do something like this:

Squared sorted array 2
Squared sorted array 2

Squared sorted array 2




Javascript code below:-


function make_squares(arr) {
const n = arr.length;
squares = Array(n).fill(0);
let highestSquareIdx = n — 1;
let left = 0,
right = n — 1;
while (left rightSquare) {
squares = leftSquare;
left += 1;
} else {
squares = rightSquare;
right -= 1;
}
highestSquareIdx -= 1;
}
return squares;
}
document.write(`Squares: ${make_squares()}
`);
document.write(`Squares: ${make_squares()}
`);



Time complexity

The time complexity of the above algorithm will be O(N) as we sorting and squaring the array in the same iteration.

Space complexity

The space complexity of the above algorithm will also be O(N); this space will be used for the output array.

READ MORE:- https://www.golibrary.co/squaring-a-sorted-array/

More From Medium

Also tagged Algorithms

Also tagged JavaScript

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade