Squares of a Sorted Array Problem

Squares of a Sorted Array Problem — ExecCode Easy DSA Practice

Solve the Squares of a Sorted Array problem on ExecCode. Free online easy DSA practice in Two Pointers. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given a sorted array that may contain negatives, return the squares in sorted order. The largest square is always at one of the two ends, so fill the answer from right to left.

Examples

Input nums = [-4,-1,0,3,10]; Output [0,1,9,16,100]. Input nums = [-7,-3,2,3,11]; Output [4,9,9,49,121]

Constraints

1 ≤ nums.length ≤ 10⁴ -10⁴ ≤ nums[i] ≤ 10⁴ nums is sorted non-decreasing

Practice Squares of a Sorted Array free on ExecCode. Browse DSA problems, topic map, and placement guides.