BackeasyArrays Accenture Zomato

Squares of Sorted Array Solution

Problem Statement

Given a sorted array of integers, compute the square of each and return them in sorted order.

Example 1
Input
[0, 3, 10]
Output
0 9 100

Explanation: Step-by-step: Given the input [0, 3, 10], we first square each number: 0^2 = 0, 3^2 = 9, 10^2 = 100. Then, we sort the squared values in ascending order: 0, 9, 100.

Example 2
Input
[-5, 2, 7]
Output
4 25 49

Explanation: Step-by-step: Given the input [-5, 2, 7], we first square each number: (-5)^2 = 25, 2^2 = 4, 7^2 = 49. Then, we sort the squared values in ascending order: 4, 25, 49.

Constraints

  • 1 <= n <= 10^4
  • -10^4 <= arr[i] <= 10^4
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free