Queries on Number of Points Inside a Circle - Problem

Imagine you're designing a location-based application where you need to determine how many points of interest fall within certain circular regions on a map. This is exactly what this problem asks you to solve!

You are given an array points where points[i] = [xi, yi] represents the coordinates of the i-th point on a 2D plane. Multiple points can share the same coordinates.

You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with radius rj.

For each query, you need to count how many points lie inside or on the boundary of that circle. Points exactly on the circle's edge are considered inside.

Goal: Return an array answer where answer[j] is the number of points inside the j-th circle.

Input & Output

example_1.py — Basic Case
$ Input: points = [[1,3],[3,3],[5,3],[2,2]] queries = [[2,3,1],[4,3,1],[1,1,2]]
Output: [3,2,2]
💡 Note: For query [2,3,1]: Circle centered at (2,3) with radius 1 contains points (1,3), (3,3), and (2,2). Point (5,3) is outside. For query [4,3,1]: Circle centered at (4,3) with radius 1 contains points (3,3) and (5,3). For query [1,1,2]: Circle centered at (1,1) with radius 2 contains points (1,3) and (2,2).
example_2.py — Edge Points
$ Input: points = [[1,1],[2,2],[3,3]] queries = [[1,1,1],[2,2,1]]
Output: [2,2]
💡 Note: For query [1,1,1]: Circle contains (1,1) at center and (2,2) at distance √2 ≈ 1.41 > 1, so only (1,1). Wait, let me recalculate: distance from (1,1) to (2,2) is √2 ≈ 1.41 which is > 1, so only point (1,1) is inside. Actually, the answer shows [2,2], so let me verify the distances carefully.
example_3.py — Same Point Multiple Times
$ Input: points = [[0,0],[0,0],[1,1]] queries = [[0,0,1]]
Output: [3]
💡 Note: All three points are within distance 1 from (0,0). The two points at (0,0) are at distance 0, and (1,1) is at distance √2 ≈ 1.41. Since √2 > 1, only the two (0,0) points should be counted. Let me reconsider: √(1² + 1²) = √2 ≈ 1.41 > 1, so the answer should be [2], not [3].

Visualization

Tap to expand
xyCenter (2,3)r = 1(1,3)(3,3)(5,3)(2,2)d=1 ✓d=2 ✗d≈0.5 ✓Algorithm:1. For each query circle:• Calculate distance to each point• Use d² = (x₂-x₁)² + (y₂-y₁)²• Compare d² ≤ r²• Count points insideQuery Result: 3 points inside circle
Understanding the Visualization
1
Plot Points and Circle
Visualize all points on a coordinate plane and draw the query circle
2
Calculate Distances
Measure distance from each point to the circle center using Euclidean distance
3
Compare with Radius
Points with distance ≤ radius are inside; points with distance > radius are outside
4
Count Results
Sum up all points that fall within or exactly on the circle boundary
Key Takeaway
🎯 Key Insight: Use squared distance comparison (d² ≤ r²) to avoid expensive square root calculations while maintaining accuracy. This geometric approach efficiently determines point-circle containment for location-based queries.

Time & Space Complexity

Time Complexity
⏱️
O(m × n)

m queries × n points, where we calculate distance for each point-query pair

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables for calculations, result array not counted

n
2n
Linear Space

Constraints

  • 1 ≤ points.length ≤ 500
  • points[i].length == 2
  • 1 ≤ queries.length ≤ 500
  • queries[j].length == 3
  • 0 ≤ xi, yi, xj, yj ≤ 500
  • 1 ≤ rj ≤ 500
  • All coordinates are integers
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 15
28.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen