Given a sorted integer array, return a new array containing the squares of each number in non-decreasing order.
Problem
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.
Goal
Produce the sorted sequence of squared values without changing the meaning of the input array.
Input Format
- An integer array
numssorted in non-decreasing order. - The array may contain negative numbers, zeros, and positive numbers.
Output Format
- Return an integer array containing
nums[i] * nums[i]for every elementi, sorted in non-decreasing order.
Constraints
1 <= nums.length$-10^{4}$ <= nums[i] <= $10^{4}$numsis sorted in non-decreasing order- The answer should fit in 32-bit signed integer range for the usual problem constraints
Example 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
Unlock deeper context for this problem
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.