Search⌘ K
AI Features

Solution: Squares of a Sorted Array

Explore how to efficiently compute and sort the squares of a sorted integer array using the two-pointer technique. This lesson helps you understand how to compare absolute values from both ends of the array and build the result from right to left for an optimal solution with O(n) time complexity and constant additional space.

Statement

You are given an integer array, nums, sorted in non-decreasing order. Your task is to return a new array containing the squares of each number, also sorted in non-decreasing order.

Constraints:

  • 11 \leq nums.length 103\leq 10^3 ...