Search⌘ K
AI Features

Solution: Squares of a Sorted Array

Explore the two pointers technique to solve the problem of squaring each number in a sorted array and returning a new sorted array. Learn how to compare absolute values from both ends, fill the result array from right to left, and understand the optimal O(n) time solution with O(1) extra 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 ...