...

/

Squaring a Sorted Array (easy)

Squaring a Sorted Array (easy)

Problem Statement

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

Example 1:

Input: [-2, -1, 0, 2, 3]
Output: [0, 1, 4, 4, 9]

Example 2:

Input: [-3, -1, 0, 1, 2]
Output: [0, 1, 1, 4, 9]

Try it yourself

Try solving this question here:

class SortedArraySquares {
public static int[] makeSquares(int[] arr) {
int[] squares = new int[arr.length];
// TODO: Write your code here
return squares;
}
}

Solution

This is a straightforward question. The only trick is that we can have negative numbers in the input array, which will make it a bit difficult to ...