Find the most points enclosed in a fixed size circle
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
Finding the maximum number of points that fit inside a circle of fixed radius is a classic computational-geometry problem. The useful insight is that an optimal circle can be positioned so that at least one or two input points lie on its boundary, which turns an infinite search space into a finite algorithm.
Key Geometric Idea
If a circle of radius r encloses the best possible set of points, you do not need to test every center in the plane. For an optimal solution, you can focus on circles whose boundary passes through one point or through two points.
Why two points matter:
- If two points are farther apart than
2r, no circle of radiusrcan contain both on its boundary. - If two points are within distance
2r, there are up to two circle centers of radiusrthat pass through both points.
So a practical exact algorithm is:
- For each point pair within distance
2r, compute candidate circle centers. - Count how many points lie inside each candidate circle.
- Track the maximum count.
This is not the most asymptotically optimal approach, but it is clear and works well for many input sizes.
Computing Candidate Centers
Suppose the points are p1 and p2. Their midpoint gives a baseline, and the candidate centers lie on the perpendicular bisector at a distance determined by the radius.
Here is a complete Python implementation:
The helper count_points checks how many input points fall inside or on the circle boundary.
Complexity
This implementation examines all point pairs and, for each candidate center, scans all points to count membership. That gives roughly O(n^3) time in the straightforward form.
For moderate input sizes, this is often acceptable and much easier to implement correctly than more advanced angular sweep algorithms. If n becomes very large, then specialized O(n^2 log n) methods or spatial indexing become more attractive.
Precision Matters
Geometry code is sensitive to floating-point noise. In the example above, the containment test uses a tiny epsilon:
Without that tolerance, points that are mathematically on the boundary can be excluded because of rounding error.
That matters especially when:
- many points lie near the circle edge
- coordinates are large
- the input comes from previous floating-point computation
When a Simpler Approximation Is Enough
If you are doing visualization, rough clustering, or an interactive UI tool, you may not need an exact optimum. A grid search over candidate centers can be simpler to code, though less precise.
For interview problems or algorithmic correctness, however, the pair-of-points construction is the more defensible exact method.
Common Pitfalls
The biggest mistake is checking only circles centered on the existing points. The optimal circle center usually lies somewhere between points, not necessarily on one of them.
Another issue is forgetting that a pair of points farther apart than 2r cannot lie on the same circle of radius r. Skipping that check leads to invalid square roots and broken geometry.
Developers also underestimate floating-point error. Without a small epsilon, boundary cases can fail unexpectedly.
Finally, brute-forcing every possible center on a continuous plane is conceptually wrong. The whole problem becomes tractable only after reducing the candidate set using geometry.
Summary
- An optimal fixed-radius circle can be found by testing centers derived from point pairs.
- Only pairs within distance
2rcan define a valid circle of radiusr. - For each valid pair, there are up to two candidate centers.
- A straightforward exact implementation is about
O(n^3)and is often practical for moderate inputs. - Use a small epsilon in boundary checks to avoid floating-point surprises.
Related reading
- Find the nearest value with a non-linear progression
- Find the next greater element in an array
- Find the number of elements greater than x in a given range
- Find the number of nodes of n-element heap of given height
- Find the number of remove-then-append operations needed to sort a given array
- Find the optimal clipped circle
- Find the shortest fence that encloses an area on a 2D grid
- Find the simplest rational number between two given rational numbers

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.