Haskell Tape Equilibrium
tapeEquilibrium :: [Int] -> Int
tapeEquilibrium a = result
where
(_, _, result) = foldl step (0, sum a, maxBound) [0 .. length a - 2]
step (firstPart, secondPart, best) i =
let firstPart' = firstPart + i
secondPart' = secondPart - i
diff = abs (firstPart' - secondPart')
in (firstPart', secondPart', min best diff)
This keeps left and right running sums and updates the smallest difference at each split point.