Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

30. Squares of a Sorted Array

EasyDay 9 of 60Two Pointers

Given an integer array nums sorted in non-decreasing order, which may contain negative numbers, return a new array with the square of each number, also sorted in non-decreasing order.

Example 1
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
The squares are [16,1,0,9,100], which sorted become [0,1,9,16,100].
Example 2
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Constraints
  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • `nums` is sorted in non-decreasing order
Hints
Frequently asked at: Meta, Amazon, Uber, Apple
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min