12 · Two Pointers

#977 Squares of a Sorted Array

Easysolve on LeetCode ↗

frequently reported atUberMetaAmazonGoogleMicrosoft

the problem

In plain words

nums is sorted in non-decreasing order and may contain negative numbers. Return a new array holding the square of every number, also sorted in non-decreasing order.

in nums = [-4,-1,0,3,10]

out [0,1,9,16,100]

Squares are [16,1,0,9,100]; sorted: [0,1,9,16,100].

in nums = [-7,-3,2,3,11]

out [4,9,9,49,121]

  • 1 ≤ n ≤ 10⁴
  • -10⁴ ≤ nums[i] ≤ 10⁴
  • Sorted non-decreasing
  • Follow-up: O(n)

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #88
      Merge Sorted Array
      Easyin Two Pointers
    2. #167
    3. #11
      Container With Most Water
      Mediumin Two Pointers