Loading content...
You are given an integer array nums that is already arranged in ascending order (non-decreasing from left to right). The array may contain negative numbers, zero, and positive numbers.
Your task is to compute the square of each element in the array and return a new array containing these squared values, sorted in ascending order.
While the original array is sorted, squaring negative numbers can disrupt this ordering (since the square of a large negative number becomes a large positive number). Your solution should efficiently produce the final sorted result.
nums = [-4,-1,0,3,10][0,1,9,16,100]After squaring each element, we get [16, 1, 0, 9, 100]. Sorting these squared values in ascending order gives [0, 1, 9, 16, 100].
nums = [-7,-3,2,3,11][4,9,9,49,121]After squaring: [49, 9, 4, 9, 121]. Sorting in ascending order produces [4, 9, 9, 49, 121]. Notice that duplicate squared values (9 appears twice) are preserved.
nums = [1,2,3,4,5][1,4,9,16,25]Since all elements are positive and the array is already sorted, the squared values [1, 4, 9, 16, 25] are automatically in ascending order.
Constraints