class Solution {
public int[] sortedSquares(int[] arr) {
for(int i = 0; i < arr.length; i++){
for(int j=i+1;j<arr.length;j++){
arr[i]=arr[i]*arr[i];
arr[j]=arr[j]*arr[j];
if(arr[j]<arr[i]){
int temp=0;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return arr;
}
}
Can you tell me where am going wrong? For input [-4,-1,0,3,10]
, output should be [0,1,9,16,100]
. My current output is [0,1,43046721,0,1874919424]
. I don't want to use Array.sort()
. I wanted to write my own code.