#K10696. Sorted Squares Array
Sorted Squares Array
Sorted Squares Array
You are given an integer array nums
of size n which is sorted in non-decreasing order. Your task is to compute a new array in which each element is the square of the corresponding element in nums
, and then return the new array sorted in non-decreasing order.
Example:
Input: n = 5 nums = [-4, -1, 0, 3, 10] Output: 0 1 9 16 100
The equation for squaring a number is given by \(x^2\) for any element \(x\) in the array.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer
n
, representing the number of elements in the array. - The second line contains
n
space-separated integers sorted in non-decreasing order.
outputFormat
Print a single line to standard output (stdout) containing n
space-separated integers which are the squares of the input array elements, sorted in non-decreasing order.
5
-4 -1 0 3 10
0 1 9 16 100