Produce the Sorted List of Squared Values

Implement sortedSquaredValues

Given an integer array nums sorted in non-decreasing order (it may contain negative values), return a new array holding the square of every element, with the result itself sorted in non-decreasing order. Squaring can scramble the order — a large negative number produces a large positive square. Since the input is already sorted, you can rebuild the sorted result in a single O(n) pass with two pointers instead of squaring and re-sorting from scratch.

Example 1:

Input: nums = [-6,-3,-1,2,5]

Output: [1,4,9,25,36]

Example 2:

Input: nums = [-2,0,3]

Output: [0,4,9]

Example 3:

Input: nums = [1,2,4]

Output: [1,4,16]

+ 4 hidden test cases run on Submit.

Constraints:

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

nums =

[-6, -3, -1, 2, 5]