Squares of a Sorted Array

nums is sorted from smallest to largest and may contain negative numbers. Return the squares of its numbers, also sorted from smallest to largest.

Constraints

  • 1 ≤ length of nums ≤ 10,000
  • -10,000 ≤ nums[i] ≤ 10,000
  • nums is sorted in non-decreasing order.

Example 1

Input
nums = [-4,-1,0,3,10]
Output
[0,1,9,16,100]
Why:

The squares are 16, 1, 0, 9, 100; sorted, 0, 1, 9, 16, 100.

Example 2

Input
nums = [-7,-3,2,3,11]
Output
[4,9,9,49,121]
Why:

-3 and 3 both give 9.

Hints

    Was this a good challenge?

    Solved by
    0
    Submissions
    0
    Accepted
    –