Given a sorted integer array, return a new array containing the squares of each number in non-decreasing order.
You are given a sorted integer array nums in non-decreasing order. Some values may be negative.
Return a new array consisting of the square of each number, also in non-decreasing order.
Because squaring can change the order of negative and positive numbers, the result should be arranged after all values are squared.
Produce the sorted sequence of squared values without changing the meaning of the input array.
nums sorted in non-decreasing order.nums[i] * nums[i] for every element i, sorted in non-decreasing order.1 <= nums.length$-10^{4}$ <= nums[i] <= $10^{4}$nums is sorted in non-decreasing orderExample 1
Input
nums = [-4,-1,0,3,10]
Output
[0,1,9,16,100]
Explanation
After squaring, the values are [16,1,0,9,100]. Sorting them gives [0,1,9,16,100].
Example 2
Input
nums = [-7,-3,2,3,11]
Output
[4,9,9,49,121]
Explanation
The squared values are [49,9,4,9,121], which in sorted order become [4,9,9,49,121].
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.