You receive an integer array already sorted in non-decreasing order. Produce a new array containing the square of every element, arranged from smallest to largest.
The challenge is that negative numbers can square into large values, so the largest squares live at the two ends of the input rather than in the middle.
Example 1
Input: nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]
Squaring gives 16, 1, 0, 9, 100, which sorts into the shown result.
Example 2
Input: nums = [-7, -3, 2, 3, 11]
Output: [4, 9, 9, 49, 121]
The largest square, 121, comes from the rightmost value while 49 comes from the leftmost.
Constraints
1 ≤ nums.length ≤ 10^4-10^4 ≤ nums[i] ≤ 10^4nums is sorted in non-decreasing orderSee the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI