Search⌘ K
AI Features

Solution: Squares of a Sorted Array

Explore how to efficiently compute the squares of a sorted integer array and return them in sorted order. This lesson teaches the two-pointer approach that compares absolute values from both ends of the array to build the result from the highest square downward, achieving an O(n) time complexity and constant space complexity apart from the output. By mastering this technique, you will understand a practical pattern for solving similar array problems efficiently in coding interviews.

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 ...