Python Tape Equilibrium
def tape_equilibrium(a: list[int]) -> int:
first_part = 0
second_part = sum(a)
min_diff = float("inf")
for i in range(len(a) - 1):
first_part += i
second_part -= i
diff = abs(first_part - second_part)
min_diff = min(min_diff, diff)
return int(min_diff)
This keeps left and right running sums and updates the smallest difference at each split point.
Python Triangle
def triangle(a: list[int]) -> int:
a = sorted(a)
c = len(a)
if c < 3:
return 0
for i in range(c - 2):
if a[i] > 0 and a[i] > (a[i + 2] - a[i + 1]):
return 1
return 0
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.