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.
Haskell Triangle
import Data.List (sort)
triangle :: [Int] -> Int
triangle a
| length s < 3 = 0
| any valid (zip3 s (drop 1 s) (drop 2 s)) = 1
| otherwise = 0
where
s = sort a
valid (x, y, z) = x > 0 && x > (z - y)
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.