Search⌘ K
AI Features

Solution: Squares of a Sorted Array

Explore how to apply the two pointers pattern to efficiently generate a sorted array of squared integers from a sorted input. Understand the key observation that the largest squared value comes from extremes and learn to fill the output array in one pass while maintaining O(n) time and constant space complexity.

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