Similar idea.

class Solution {
public int findMinMoves(int[] machines) {
int n=machines.length;
int total=0;
for (int num:machines)
total += num;
if (total%n != 0) return -1;
int avg=total/n;
int[] diffL=new int[n];
int sum=0;
for (int i=0;i<n;i++){
sum += machines[i];
diffL[i]=sum-(i+1)*avg;
}
int[] diffR=new int[n];
sum=0;
for (int i=n-1;i>=0;i--){
sum += machines[i];
diffR[i]=sum-(n-i)*avg;
}
int max=0;
for (int i=0;i<n;i++){
int steps=Math.max(Math.abs(diffL[i]), Math.abs(diffR[i]));
if (diffL[i]>0 && diffR[i]>0)
steps=Math.max(steps, diffL[i]+diffR[i]);
max=Math.max(max, steps);
}
return max;
}
}