class Solution {
public int[] sortedSquares(int[] A) {
int n = A.length;
int[] res = new int[n];
int j = 0;
int t = 0;
while(j<n && A[j]<0){
j++;
}
int i=j-1;
while(j<n&&i>=0){
if(A[i]*A[i]<A[j]*A[j]){
res[t++]=A[i]*A[i];
i--;
}
else{
res[t++]=A[j]*A[j];
j++;
}
}
while (i >= 0) {
res[t++] = A[i] * A[i];
i--;
}
while (j < n) {
res[t++] = A[j] * A[j];
j++;
}
return res;
}
}