Algorithm to calculate number of intersecting discs
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The intersecting-discs problem asks how many disc pairs overlap when disc i is centered at index i with radius A[i]. A brute-force check of every pair is too slow for large input. The standard efficient solution converts each disc into an interval and counts overlaps with a sweep-style algorithm.
Convert discs into intervals
Disc i spans from i - A[i] to i + A[i]. Two discs intersect when those intervals overlap.
For example, if A = [1, 5, 2]:
- disc
0spans-1to1 - disc
1spans-4to6 - disc
2spans0to4
Once you think in intervals, the geometry becomes much simpler. You are no longer comparing circles directly. You are comparing start and end points on a line.
Why the naive solution is too slow
The obvious method compares every disc with every later disc:
This is O(N^2), which is far too slow for typical interview or challenge constraints. The expected approach is usually O(N log N).
Sort starts and ends separately
The efficient solution builds two arrays:
- '
starts: every disc's left boundary' - '
ends: every disc's right boundary'
Then sort both arrays and walk through them.
That example returns 11.
Why the sweep logic works
Think of the sorted boundaries as events:
- a start event means a new disc opens
- an end event means a disc closes
When a new disc opens, it intersects every disc that is already open. So if open_discs is currently 3, the new disc contributes 3 new intersections immediately.
After counting those new pairs, increment open_discs because the new disc is now active too. When you pass an end boundary, decrement open_discs because one disc is no longer active.
This avoids checking pairs explicitly. Instead of asking whether disc i intersects disc j, you count intersections in groups using how many discs are currently open.
Handle boundary details correctly
There are a few details that matter:
- touching counts as intersecting, so use
<=rather than< - boundaries can be large, so use a wide numeric type in fixed-width languages
- some versions of the problem require returning
-1if the count exceeds10_000_000
That last rule is why the early return appears inside the counting loop rather than after everything finishes.
In Java or C#, store interval endpoints in long rather than int if the radius values may be large.
This is a sweep-line pattern, not a geometry trick
The deeper lesson is not just about discs. This is a classic sweep-line or event-counting pattern:
- turn each object into start and end events
- sort the events
- count how many objects are active as you sweep
The same idea appears in interval overlap problems, meeting-room scheduling, maximum concurrent sessions, and similar tasks.
Common Pitfalls
The most common mistake is sticking with the quadratic pairwise solution when the problem clearly needs better performance.
Another common issue is forgetting that intervals that only touch at a boundary still count as intersections in the standard version of the problem.
People also update the active-disc counter in the wrong order. The count should be added before the new disc is marked open, because the new disc intersects the discs that were already active.
Finally, fixed-width languages can overflow if the endpoints are stored in a type that is too small.
Summary
- Model each disc as an interval from
i - A[i]toi + A[i]. - Sort interval starts and ends separately.
- Count intersections by tracking how many discs are currently open.
- The efficient solution runs in
O(N log N)because sorting dominates the work. - The core idea is a sweep-line event algorithm, not pairwise geometry checks.

