Since we need Ax<=Ay<=Az
firstly, I had sorted the array, and then,
I had taken Ax as a minimum element
Az as a maximum element
and Ay as the median of the array.
if(N is even) Ay = (A[(n-1)/2] + A[(n+1)/2])/2;
if(N is odd) Ay = (A[(n/2)]);
thus, ans = abs(Az - Ax) + abs(Ay - Ax) + abs(Az - Ay);