# Squares of a Sorted Array (Leetcode 977)

Problem Link: <https://leetcode.com/problems/squares-of-a-sorted-array/>

```python
class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        
        n=len(nums)
        i=0
        ans=[]
        while(i<n):
            if(nums[i]==0): # If there is a zero in the array
                
                ans.append(0)
                neg=i-1
                pos=i+1
                while(neg>=0 and pos<n):
                    if(abs(nums[neg])<=abs(nums[pos])):
                        ans.append(nums[neg]*nums[neg])
                        neg-=1
                    else:
                        ans.append(nums[pos]*nums[pos])
                        pos+=1
                
                while(neg>=0):
                    ans.append(nums[neg]*nums[neg])
                    neg-=1
                
                while(pos<n):
                    ans.append(nums[pos]*nums[pos])
                    pos+=1
                    
                break
                    
            elif(nums[i]>0): # If there is no zero in the array
                
                neg=i-1
                pos=i
                while(neg>=0 and pos<n):
                    if(abs(nums[neg])<=abs(nums[pos])):
                        ans.append(nums[neg]*nums[neg])
                        neg-=1
                    else:
                        ans.append(nums[pos]*nums[pos])
                        pos+=1
                
                while(neg>=0):
                    ans.append(nums[neg]*nums[neg])
                    neg-=1
                
                while(pos<n):
                    ans.append(nums[pos]*nums[pos])
                    pos+=1
                    
                break
                    
            i+=1
            
        if(i==n): # If all are negative numbers
            
            for i in range(n-1,-1,-1):
                ans.append(nums[i]*nums[i])
                
        return ans
```
