Python Number Of Disc Intersections
def number_of_disc_intersections(a: list[int]) -> int:
c = len(a)
start = [0] * c
end = [0] * c
for k, v in enumerate(a):
key = 0 if k < v else k - v
start[key] += 1
key = c - 1 if k + v >= c else k + v
end[key] += 1
total = 0
active = 0
for k in range(c):
total += active * start[k] + (start[k] * (start[k] - 1)) // 2
active += start[k] - end[k]
if total > 10000000:
return -1
return total
This sorts disc start and end points and counts active overlaps without comparing every pair directly.