class Solution {
public int[] sortedSquares(int[] nums) {
int[] res = new int[nums.length];
int l = 0, r = nums.length - 1;
int resP = res.length - 1;
while (l <= r) {
int lSquare = nums[l] * nums[l];
int rSquare = nums[r] * nums[r];
if (lSquare >= rSquare) {
res[resP] = lSquare;
++l;
} else if (lSquare < rSquare) {
res[resP] = rSquare;
--r;
}
--resP;
}
return res;
}
}