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 CirclesGrid[2,2,1][3,4,1]Result: Count all red dots (unique lattice points)
Understanding the Visualization
1
Input
Circles with center coordinates and radius
2
Process
Find all lattice points inside any circle
3
Output
Count of unique lattice points
Key Takeaway
🎯 Key Insight: Use distance formula (x-cx)² + (y-cy)² ≤ r² to check if lattice points are inside circles, and handle overlaps with a set
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