Squares of a Sorted Array

Given an integer array nums sorted in non-decreasing order, return an array containing the square of every value, also sorted in non-decreasing order. The…

Given an integer array `nums` sorted in non-decreasing order, return an array containing the square of every value, also sorted in non-decreasing order. The returned array must contain exactly one square for each input value.

- `1 <= nums.length <= 10^4` - `-10^4 <= nums[i] <= 10^4` - `nums` is sorted in non-decreasing order. - The interactive visualizer accepts at most 12 values; longer valid arrays are execution-only.

Example 1: input nums = [-4,-1,0,3,10]; output [0,1,9,16,100]. The squares are [16,1,0,9,100], which sort to [0,1,9,16,100].

Example 2: input nums = [-7,-3,2,3,11]; output [4,9,9,49,121]. Equal magnitudes can create duplicate squares.

Example 3: input nums = [-5,-4,-3]; output [9,16,25]. For all-negative sorted input, square order is the reverse of value order.

Category: Two Pointers. Topic: two-pointers. Difficulty: Easy. Patterns: two-pointers, sorting.