Count Lattice Points Inside a Circle - Problem

Given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and radius ri of the ith circle drawn on a grid, return the number of lattice points that are present inside at least one circle.

A lattice point is a point with integer coordinates. Points that lie on the circumference of a circle are also considered to be inside it.

Input & Output

Example 1 — Two Circles with Overlap
$ Input: circles = [[2,2,1],[3,4,1]]
Output: 16
💡 Note: Circle 1 centered at (2,2) with radius 1 covers points like (1,2), (2,1), (2,2), (2,3), (3,2). Circle 2 centered at (3,4) with radius 1 covers points like (2,4), (3,3), (3,4), (3,5), (4,4). Total unique lattice points is 16.
Example 2 — Single Small Circle
$ Input: circles = [[1,1,1]]
Output: 5
💡 Note: Circle centered at (1,1) with radius 1 covers lattice points: (0,1), (1,0), (1,1), (1,2), (2,1). Total is 5 points.
Example 3 — Multiple Overlapping Circles
$ Input: circles = [[1,1,1],[2,2,1],[3,3,1]]
Output: 12
💡 Note: Three circles with radius 1 each, positioned diagonally. Some points are covered by multiple circles but counted only once. Total unique lattice points is 12.

Constraints

  • 1 ≤ circles.length ≤ 200
  • circles[i].length == 3
  • -100 ≤ xi, yi ≤ 100
  • 1 ≤ ri ≤ min(xi, yi) + 100

Visualization

Tap to expand
Count Lattice Points Inside a Circle INPUT 0 1 2 3 4 5 circles = [ [2, 2, 1], [3, 4, 1] ] Circle 1 (2,2,1) Circle 2 (3,4,1) [xi, yi, ri] = center + radius ALGORITHM STEPS 1 Initialize Set Create empty set for points 2 For Each Circle Iterate through circles array 3 Check Lattice Points Test (x-xi)^2 + (y-yi)^2 <= ri^2 4 Add to Set Set avoids duplicates Set of Points Found: (1,2) (2,1) (2,2) (2,3) (3,2) (2,4) (3,3) (3,4) (3,5) (4,4) Bounding box scan: x: [xi-ri, xi+ri] y: [yi-ri, yi+ri] Return set.size() FINAL RESULT OUTPUT 16 16 unique lattice points inside at least one circle = Counted lattice point Key Insight: Using a Set automatically handles overlapping regions - points inside multiple circles are only counted once. For each circle, scan its bounding box [xi-ri, xi+ri] x [yi-ri, yi+ri] and check distance formula: (x - xi)^2 + (y - yi)^2 <= ri^2 --> point (x,y) is inside or on circle i TutorialsPoint - Count Lattice Points Inside a Circle | Circle-by-Circle with Set Approach
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~25 min Avg. Time
340 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