Algorithm to calculate number of intersecting discs
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Algorithm to calculate the most energy efficient ad-hoc network
- Algorithm to calculate the number of 1s for a range of numbers in binary
- Algorithm to calculate the number of combinations to form 100
- Algorithm to calculate the number of divisors of a given number
- Algorithm to Calculate the Number of Lattice Points in a Polygon
- Algorithm to check if two boxes overlap
- Algorithm to check similarity of colors
- Algorithm to classify a list of products?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.