TypeScript Tape Equilibrium
function tapeEquilibrium(a: number[]): number {
let firstPart = 0;
let secondPart = a.reduce((sum, v) => sum + v, 0);
let min = Infinity;
for (let i = 0; i < a.length - 1; i++) {
firstPart += i;
secondPart -= i;
const difference = Math.abs(firstPart - secondPart);
min = difference < min ? difference : min;
}
return min;
}
This keeps left and right running sums and updates the smallest difference at each split point.