Java Tape Equilibrium
public class Solution {
public static int tapeEquilibrium(int[] a) {
long firstPart = 0;
long secondPart = 0;
for (int v : a) {
secondPart += v;
}
long min = Long.MAX_VALUE;
for (int i = 0; i < a.length - 1; i++) {
firstPart += i;
secondPart -= i;
long difference = Math.abs(firstPart - secondPart);
min = Math.min(min, difference);
}
return (int) min;
}
}
This keeps left and right running sums and updates the smallest difference at each split point.