# Blogs Hub

### by Sumit Chourasia | Sep 27, 2020 | Category :coding | Tags : algorithmarrayडेटा-संरचनाआसानleetcode

#### Squares of a Sorted Array - Array - Easy - LeetCode - मिनी टीवी

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.

Example 1:

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

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

Note:

1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A is sorted in non-decreasing order.

``````public class Solution {
public int[] SortedSquares(int[] A) {
var stack = new Stack<int>();
var pos = new int[A.Length];
var posLen = 0;
int i = 0;
for(i=0;i<A.Length;i++){
if(A[i]<0){
stack.Push(A[i]*A[i]);
}
else{
pos[posLen]=A[i]*A[i];
posLen++;
}
}

i=0;
int j=0;
while(stack.Count>0){
if(posLen==0 || j>=posLen){
A[i]=stack.Pop();
}
else if(stack.Peek()<pos[j]){
A[i]=stack.Pop();
}
else{
A[i]=pos[j];
j++;
}
i++;
}

while(j<posLen){
A[i]=pos[j];
j++;
i++;
}

return A;
}
}``````

Time Complexity: O(n)

Space Complexity: O(n)

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia