left
at the start of the array and right
at the end of the array.result
of the same length as nums
to store the squares in non-decreasing order.result
array to the beginning.
a. Compare the absolute values of nums[left]
and nums[right]
.
b. If abs(nums[left])
is greater than abs(nums[right])
, square nums[left]
and place it in the current position of result
, then increment left
.
c. Otherwise, square nums[right]
and place it in the current position of result
, then decrement right
.result
are filled.result
array.