Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 ith 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.
Example 1:
Input: darts = [[-2,0],[2,0],[0,2],[0,-2]], r = 2 Output: 4 Explanation: Circle dartboard with center in (0,0) and radius = 2 contain all points.
Example 2:
Input: darts = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r = 5 Output: 5 Explanation: Circle dartboard with center in (0,4) and radius = 5 contain all points except the point (7,8).
Constraints:
1 <= darts.length <= 100darts[i].length == 2-104 <= xi, yi <= 104darts are unique1 <= r <= 5000Problem summary: 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 ith 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[-2,0],[2,0],[0,2],[0,-2]] 2
[[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]] 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
class Solution {
public int numPoints(int[][] darts, int r) {
int n = darts.length;
int maxDarts = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
List<double[]> centers
= possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1], r);
for (double[] center : centers) {
maxDarts = Math.max(maxDarts, countDarts(center[0], center[1], darts, r));
}
}
}
return maxDarts;
}
private List<double[]> possibleCenters(int x1, int y1, int x2, int y2, int r) {
List<double[]> centers = new ArrayList<>();
double dx = x2 - x1;
double dy = y2 - y1;
double d = Math.sqrt(dx * dx + dy * dy);
if (d > 2 * r) {
return centers;
}
double midX = (x1 + x2) / 2.0;
double midY = (y1 + y2) / 2.0;
double distToCenter = Math.sqrt(r * r - (d / 2.0) * (d / 2.0));
double offsetX = distToCenter * dy / d;
double offsetY = distToCenter * -dx / d;
centers.add(new double[] {midX + offsetX, midY + offsetY});
centers.add(new double[] {midX - offsetX, midY - offsetY});
return centers;
}
private int countDarts(double x, double y, int[][] darts, int r) {
int count = 0;
for (int[] dart : darts) {
if (Math.sqrt(Math.pow(dart[0] - x, 2) + Math.pow(dart[1] - y, 2)) <= r + 1e-7) {
count++;
}
}
return count;
}
}
// Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
// class Solution {
// public int numPoints(int[][] darts, int r) {
// int n = darts.length;
// int maxDarts = 1;
//
// for (int i = 0; i < n; i++) {
// for (int j = i + 1; j < n; j++) {
// List<double[]> centers
// = possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1], r);
// for (double[] center : centers) {
// maxDarts = Math.max(maxDarts, countDarts(center[0], center[1], darts, r));
// }
// }
// }
// return maxDarts;
// }
//
// private List<double[]> possibleCenters(int x1, int y1, int x2, int y2, int r) {
// List<double[]> centers = new ArrayList<>();
// double dx = x2 - x1;
// double dy = y2 - y1;
// double d = Math.sqrt(dx * dx + dy * dy);
// if (d > 2 * r) {
// return centers;
// }
// double midX = (x1 + x2) / 2.0;
// double midY = (y1 + y2) / 2.0;
// double distToCenter = Math.sqrt(r * r - (d / 2.0) * (d / 2.0));
// double offsetX = distToCenter * dy / d;
// double offsetY = distToCenter * -dx / d;
//
// centers.add(new double[] {midX + offsetX, midY + offsetY});
// centers.add(new double[] {midX - offsetX, midY - offsetY});
// return centers;
// }
//
// private int countDarts(double x, double y, int[][] darts, int r) {
// int count = 0;
// for (int[] dart : darts) {
// if (Math.sqrt(Math.pow(dart[0] - x, 2) + Math.pow(dart[1] - y, 2)) <= r + 1e-7) {
// count++;
// }
// }
// return count;
// }
// }
# Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
class Solution:
def numPoints(self, darts: list[list[int]], r: int) -> int:
def countDarts(x, y):
count = 0
for x1, y1 in darts:
if dist((x, y), (x1, y1)) <= r + 1e-7:
count += 1
return count
def possibleCenters(x1, y1, x2, y2):
dx, dy = x2 - x1, y2 - y1
d = sqrt(dx * dx + dy * dy)
if d > 2 * r:
return []
mid_x, mid_y = (x1 + x2) / 2, (y1 + y2) / 2
dist_to_center = sqrt(r * r - (d / 2) * (d / 2))
offset_x = dist_to_center * dy / d
offset_y = dist_to_center * -dx / d
return [
(mid_x + offset_x, mid_y + offset_y),
(mid_x - offset_x, mid_y - offset_y),
]
n = len(darts)
max_darts = 1
for i in range(n):
for j in range(i + 1, n):
centers = possibleCenters(
darts[i][0], darts[i][1], darts[j][0], darts[j][1]
)
for center in centers:
max_darts = max(max_darts, countDarts(center[0], center[1]))
return max_darts
// Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
/**
* [1453] Maximum Number of Darts Inside of a Circular Dartboard
*
* 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.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/04/29/sample_1_1806.png" style="width: 248px; height: 211px;" />
* Input: darts = [[-2,0],[2,0],[0,2],[0,-2]], r = 2
* Output: 4
* Explanation: Circle dartboard with center in (0,0) and radius = 2 contain all points.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/04/29/sample_2_1806.png" style="width: 306px; height: 244px;" />
* Input: darts = [[-3,0],[3,0],[2,6],[5,4],[0,9],[7,8]], r = 5
* Output: 5
* Explanation: Circle dartboard with center in (0,4) and radius = 5 contain all points except the point (7,8).
*
*
* Constraints:
*
* 1 <= darts.length <= 100
* darts[i].length == 2
* -10^4 <= xi, yi <= 10^4
* All the darts are unique
* 1 <= r <= 5000
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/
// discuss: https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
// Credit: https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/solutions/3146699/just-a-runnable-solution/
pub fn num_points(darts: Vec<Vec<i32>>, r: i32) -> i32 {
let mut result = 1;
let r = r as f64;
for i in 0..darts.len() - 1 {
for j in i + 1..darts.len() {
let x1 = darts[i][0] as f64;
let y1 = darts[i][1] as f64;
let x2 = darts[j][0] as f64;
let y2 = darts[j][1] as f64;
let d = ((x1 - x2).powi(2) + (y1 - y2).powi(2)) / 4.0;
if d > r.powi(2) {
continue;
}
let x0 = (x1 + x2) / 2.0 + (y2 - y1) * (r.powi(2) - d).sqrt() / (d * 4.0).sqrt();
let y0 = (y1 + y2) / 2.0 - (x2 - x1) * (r.powi(2) - d).sqrt() / (d * 4.0).sqrt();
let mut cnt = 0;
for point in &darts {
let x = point[0] as f64;
let y = point[1] as f64;
if (x - x0).powi(2) + (y - y0).powi(2) <= r.powi(2) + 0.00001 {
cnt += 1;
}
}
result = result.max(cnt);
let x0 = (x1 + x2) / 2.0 - (y2 - y1) * (r.powi(2) - d).sqrt();
let y0 = (y1 + y2) / 2.0 + (x2 - x1) * (r.powi(2) - d).sqrt();
let mut cnt = 0;
for point in &darts {
let x = point[0] as f64;
let y = point[1] as f64;
if (x - x0).powi(2) + (y - y0).powi(2) <= r.powi(2) + 0.00001 {
cnt += 1;
}
}
result = result.max(cnt);
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1453_example_1() {
let darts = vec![vec![-2, 0], vec![2, 0], vec![0, 2], vec![0, -2]];
let r = 2;
let result = 4;
assert_eq!(Solution::num_points(darts, r), result);
}
#[test]
fn test_1453_example_2() {
let darts = vec![
vec![-3, 0],
vec![3, 0],
vec![2, 6],
vec![5, 4],
vec![0, 9],
vec![7, 8],
];
let r = 5;
let result = 5;
assert_eq!(Solution::num_points(darts, r), result);
}
}
// Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1453: Maximum Number of Darts Inside of a Circular Dartboard
// class Solution {
// public int numPoints(int[][] darts, int r) {
// int n = darts.length;
// int maxDarts = 1;
//
// for (int i = 0; i < n; i++) {
// for (int j = i + 1; j < n; j++) {
// List<double[]> centers
// = possibleCenters(darts[i][0], darts[i][1], darts[j][0], darts[j][1], r);
// for (double[] center : centers) {
// maxDarts = Math.max(maxDarts, countDarts(center[0], center[1], darts, r));
// }
// }
// }
// return maxDarts;
// }
//
// private List<double[]> possibleCenters(int x1, int y1, int x2, int y2, int r) {
// List<double[]> centers = new ArrayList<>();
// double dx = x2 - x1;
// double dy = y2 - y1;
// double d = Math.sqrt(dx * dx + dy * dy);
// if (d > 2 * r) {
// return centers;
// }
// double midX = (x1 + x2) / 2.0;
// double midY = (y1 + y2) / 2.0;
// double distToCenter = Math.sqrt(r * r - (d / 2.0) * (d / 2.0));
// double offsetX = distToCenter * dy / d;
// double offsetY = distToCenter * -dx / d;
//
// centers.add(new double[] {midX + offsetX, midY + offsetY});
// centers.add(new double[] {midX - offsetX, midY - offsetY});
// return centers;
// }
//
// private int countDarts(double x, double y, int[][] darts, int r) {
// int count = 0;
// for (int[] dart : darts) {
// if (Math.sqrt(Math.pow(dart[0] - x, 2) + Math.pow(dart[1] - y, 2)) <= r + 1e-7) {
// count++;
// }
// }
// return count;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.