You get an integer array nums that is already sorted from smallest to largest. Square every value and return the squares, also sorted from smallest to largest.
The catch is the negative numbers: squaring flips their order, so -6 (which comes before -1) gives 36, which belongs after 1.
The squares in the original order are 36, 4, 1, 9, 25. Sorted, they read 1, 4, 9, 25, 36.
Every value is negative, so the squares come out in exactly the reverse order. Equal values keep both their squares.
-3 and 3 square to the same 9, and 0 stays 0.
Constraints
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4nums is sorted in non-decreasing order.Sorting the squares works; can you do it in a single O(n) pass instead?
Once squared, the biggest value has to come from one of the two ends of nums: either the most negative number or the most positive one.