Search⌘ K
AI Features

Solution: Squares of a Sorted Array

Explore how to solve the problem of returning the squares of a sorted array in non-decreasing order efficiently. This lesson teaches you to apply the two pointers pattern, comparing absolute values from both ends and filling a result array from the back. You will understand a linear time and constant space algorithm optimized beyond simple squaring and sorting methods.

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