Description

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Note that negative numbers squared become positive and may need to be repositioned.

Examples

Input:nums = [-4,-1,0,3,10]
Output:[0,1,9,16,100]
Explanation:

After squaring, sort the result.

Input:nums = [-7,-3,2,3,11]
Output:[4,9,9,49,121]
Explanation:

Squaring each element: 49, 9, 4, 9, 121. Sorted: [4, 9, 9, 49, 121].

Input:nums = [-2,-2,-1,1,2,2]
Output:[1,1,4,4,4,4]
Explanation:

After squaring: [-2,-2,-1,1,2,2] becomes [4,4,1,1,4,4]. Sorted in non-decreasing order: [1,1,4,4,4,4].

Constraints

  • 1 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!