Queries on Number of Points Inside a Circle - Problem

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 have the same coordinates.

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

For each query queries[j], compute the number of points inside the j-th circle. Points on the border of the circle are considered inside.

Return an array answer, where answer[j] is the answer to the j-th query.

Input & Output

Example 1 — Basic Case
$ Input: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1]]
Output: [3,2]
💡 Note: For query [2,3,1]: Points [1,3], [3,3], [2,2] are within distance 1 of center (2,3). For query [4,3,1]: Points [3,3], [5,3] are within distance 1 of center (4,3).
Example 2 — Points on Border
$ Input: points = [[1,1],[2,2],[3,3]], queries = [[1,1,1]]
Output: [2]
💡 Note: Point [1,1] is at center (distance 0), point [2,2] is at distance √2 ≈ 1.41 > 1, point [3,3] is at distance √8 ≈ 2.83 > 1. Only [1,1] and points exactly at distance 1 would count.
Example 3 — Large Radius
$ Input: points = [[1,1],[2,2],[3,3]], queries = [[2,2,2]]
Output: [3]
💡 Note: All points [1,1], [2,2], [3,3] are within distance 2 of center (2,2). Distances are √2, 0, √2 respectively, all ≤ 2.

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

Visualization

Tap to expand
Points Inside Circles: Count Points Within Query Regions(1,3)(3,3)(5,3)(2,2)Query 1: (2,3,1)Count: 3Query 2: (4,3,1)Count: 2Distance Formula: √((x₁-x₂)² + (y₁-y₂)²) ≤ radiusOutput: [3, 2]
Understanding the Visualization
1
Input
Points scattered on 2D plane and circle queries
2
Process
Calculate distance from each point to each circle center
3
Output
Count of points inside each circle
Key Takeaway
🎯 Key Insight: Use squared distance comparison to avoid expensive square root calculations while maintaining accuracy
Asked in
Google 25 Microsoft 18 Amazon 15
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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