Go Number Of Disc Intersections
func numberOfDiscIntersections(a []int) int {
c := len(a)
start := make([]int, c)
end := make([]int, c)
for k, v := range a {
key := k - v
if k < v {
key = 0
}
start[key]++
key = k + v
if k+v >= c {
key = c - 1
}
end[key]++
}
sum, active := 0, 0
for k := range a {
sum += active*start[k] + (start[k]*(start[k]-1))/2
active += start[k] - end[k]
if sum > 10000000 {
return -1
}
}
return sum
}
This sorts disc start and end points and counts active overlaps without comparing every pair directly.