#K49632. Sorted Squares of a Sorted Array
Sorted Squares of a Sorted Array
Sorted Squares of a Sorted Array
Given a sorted array containing both negative and positive integers, your task is to compute a new array where each element is the square of the corresponding element from the input array. The resulting array must be sorted in ascending order.
You are required to read the array from standard input and output the sorted squares array to standard output.
Note: The input array is already sorted, so an efficient solution using the two-pointer approach is recommended.
Mathematically, if the input array is \( a[0], a[1], \dots, a[n-1] \), you need to produce the array \( b[0], b[1], \dots, b[n-1] \) such that \( b[i] = a[i]^2 \) (after sorting \( b \) in non-decreasing order).
inputFormat
The first line of input contains an integer \( n \) representing the number of elements in the array. The second line contains \( n \) space-separated integers representing a sorted array of integers.
Example:
5 -4 -1 0 3 10
outputFormat
Output a single line containing \( n \) space-separated integers representing the squares of the input elements, sorted in ascending order.
Example Output:
0 1 9 16 100## sample
5
-4 -1 0 3 10
0 1 9 16 100