Maximum Number of Darts Inside of a Circular Dartboard - Problem

Alice is throwing n darts on a very large wall. You are given an array darts where darts[i] = [xi, yi] is the position of the i-th dart that Alice threw on the wall.

Bob knows the positions of the n darts on the wall. He wants to place a dartboard of radius r on the wall so that the maximum number of darts that Alice throws lie on the dartboard.

Given the integer r, return the maximum number of darts that can lie on the dartboard.

Note: A dart is considered to be on the dartboard if the distance from the center of the dartboard to the dart is at most r.

Input & Output

Example 1 — Basic Case
$ Input: darts = [[-2,0],[2,0],[0,2],[0,-2]], r = 2
Output: 4
💡 Note: Circle centered at (0,0) with radius 2 can cover all 4 darts positioned at distance 2 from origin
Example 2 — Partial Coverage
$ Input: darts = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r = 5
Output: 5
💡 Note: Optimal circle placement can cover at most 5 out of 6 darts given the radius constraint
Example 3 — Single Dart
$ Input: darts = [[1,2]], r = 1
Output: 1
💡 Note: With only one dart, any circle of radius 1 containing that dart covers exactly 1 dart

Constraints

  • 1 ≤ darts.length ≤ 100
  • darts[i].length == 2
  • -104 ≤ xi, yi ≤ 104
  • 1 ≤ r ≤ 5000

Visualization

Tap to expand
Maximum Darts in Circular Dartboard INPUT x y (-2,0) (2,0) (0,2) (0,-2) darts = [[-2,0],[2,0], [0,2],[0,-2]] r = 2 4 darts, radius = 2 ALGORITHM STEPS 1 Pick Two Points Try all pairs as boundary 2 Find Circle Centers 2 circles pass through pair 3 Count Darts Inside Check distance <= r 4 Track Maximum Update best count center Circle through 2 points FINAL RESULT Optimal circle at origin contains all 4 darts Output: 4 OK - Maximum darts = 4 Key Insight: The optimal dartboard must have at least 2 darts on its boundary. For any circle containing k darts, we can shift it until 2 darts touch the edge. So we iterate all pairs, compute the 1 or 2 possible circle centers of radius r passing through them, and count points inside. TutorialsPoint - Maximum Number of Darts Inside of a Circular Dartboard | Geometric Approach - Circle Through Two Points
Asked in
Google 15 Microsoft 12 Apple 8
12.5K Views
Medium Frequency
~35 min Avg. Time
234 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