# Squaring A Sorted Array

## Problem

Given an array of integers `A` sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

{% hint style="info" %}
For example:

```
Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
```

```
Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
```

{% endhint %}

## Solution

```
class Solution:
    def sortedSquares(self, A: List[int]) -> List[int]:
        squareArray = [0 for i in range(len(A))]
        
        i = 0
        j = len(A)-1
        highestIndex = len(A)-1
        
        while i <= j:
            leftSquare = A[i]*A[i]
            rightSquare = A[j]*A[j]
            
            if rightSquare >= leftSquare:
                squareArray[highestIndex] = rightSquare
                j-=1
            else:
                squareArray[highestIndex] = leftSquare
                i+=1
            highestIndex-=1
        return squareArray
            
```
