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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb.
The bombs are represented by a 0-indexed 2D integer array bombs where bombs[i] = [xi, yi, ri]. xi and yi denote the X-coordinate and Y-coordinate of the location of the ith bomb, whereas ri denotes the radius of its range.
You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. These bombs will further detonate the bombs that lie in their ranges.
Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb.
Example 1:
Input: bombs = [[2,1,3],[6,1,4]] Output: 2 Explanation: The above figure shows the positions and ranges of the 2 bombs. If we detonate the left bomb, the right bomb will not be affected. But if we detonate the right bomb, both bombs will be detonated. So the maximum bombs that can be detonated is max(1, 2) = 2.
Example 2:
Input: bombs = [[1,1,5],[10,10,5]] Output: 1 Explanation: Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.
Example 3:
Input: bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]] Output: 5 Explanation: The best bomb to detonate is bomb 0 because: - Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0. - Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2. - Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3. Thus all 5 bombs are detonated.
Constraints:
1 <= bombs.length <= 100bombs[i].length == 31 <= xi, yi, ri <= 105Problem summary: You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb. The bombs are represented by a 0-indexed 2D integer array bombs where bombs[i] = [xi, yi, ri]. xi and yi denote the X-coordinate and Y-coordinate of the location of the ith bomb, whereas ri denotes the radius of its range. You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. These bombs will further detonate the bombs that lie in their ranges. Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[2,1,3],[6,1,4]]
[[1,1,5],[10,10,5]]
[[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
minesweeper)number-of-provinces)max-area-of-island)rotting-oranges)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2101: Detonate the Maximum Bombs
class Solution {
public int maximumDetonation(int[][] bombs) {
int n = bombs.length;
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
int[] p1 = bombs[i], p2 = bombs[j];
double dist = Math.hypot(p1[0] - p2[0], p1[1] - p2[1]);
if (dist <= p1[2]) {
g[i].add(j);
}
if (dist <= p2[2]) {
g[j].add(i);
}
}
}
int ans = 0;
boolean[] vis = new boolean[n];
for (int k = 0; k < n; ++k) {
Arrays.fill(vis, false);
vis[k] = true;
int cnt = 0;
Deque<Integer> q = new ArrayDeque<>();
q.offer(k);
while (!q.isEmpty()) {
int i = q.poll();
++cnt;
for (int j : g[i]) {
if (!vis[j]) {
vis[j] = true;
q.offer(j);
}
}
}
if (cnt == n) {
return n;
}
ans = Math.max(ans, cnt);
}
return ans;
}
}
// Accepted solution for LeetCode #2101: Detonate the Maximum Bombs
func maximumDetonation(bombs [][]int) (ans int) {
n := len(bombs)
g := make([][]int, n)
for i, p1 := range bombs[:n-1] {
for j := i + 1; j < n; j++ {
p2 := bombs[j]
dist := math.Hypot(float64(p1[0]-p2[0]), float64(p1[1]-p2[1]))
if dist <= float64(p1[2]) {
g[i] = append(g[i], j)
}
if dist <= float64(p2[2]) {
g[j] = append(g[j], i)
}
}
}
for k := 0; k < n; k++ {
q := []int{k}
vis := make([]bool, n)
vis[k] = true
cnt := 0
for len(q) > 0 {
i := q[0]
q = q[1:]
cnt++
for _, j := range g[i] {
if !vis[j] {
vis[j] = true
q = append(q, j)
}
}
}
if cnt == n {
return n
}
ans = max(ans, cnt)
}
return
}
# Accepted solution for LeetCode #2101: Detonate the Maximum Bombs
class Solution:
def maximumDetonation(self, bombs: List[List[int]]) -> int:
n = len(bombs)
g = [[] for _ in range(n)]
for i in range(n - 1):
x1, y1, r1 = bombs[i]
for j in range(i + 1, n):
x2, y2, r2 = bombs[j]
dist = hypot(x1 - x2, y1 - y2)
if dist <= r1:
g[i].append(j)
if dist <= r2:
g[j].append(i)
ans = 0
for k in range(n):
vis = {k}
q = [k]
for i in q:
for j in g[i]:
if j not in vis:
vis.add(j)
q.append(j)
if len(vis) == n:
return n
ans = max(ans, len(vis))
return ans
// Accepted solution for LeetCode #2101: Detonate the Maximum Bombs
/**
* [2101] Detonate the Maximum Bombs
*
* You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb.
* The bombs are represented by a 0-indexed 2D integer array bombs where bombs[i] = [xi, yi, ri]. xi and yi denote the X-coordinate and Y-coordinate of the location of the i^th bomb, whereas ri denotes the radius of its range.
* You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range. These bombs will further detonate the bombs that lie in their ranges.
* Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-3.png" style="width: 300px; height: 300px;" />
* Input: bombs = [[2,1,3],[6,1,4]]
* Output: 2
* Explanation:
* The above figure shows the positions and ranges of the 2 bombs.
* If we detonate the left bomb, the right bomb will not be affected.
* But if we detonate the right bomb, both bombs will be detonated.
* So the maximum bombs that can be detonated is max(1, 2) = 2.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-2.png" style="width: 300px; height: 300px;" />
* Input: bombs = [[1,1,5],[10,10,5]]
* Output: 1
* Explanation:
* Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.
*
* Example 3:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/desmos-eg1.png" style="width: 300px; height: 300px;" />
* Input: bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
* Output: 5
* Explanation:
* The best bomb to detonate is bomb 0 because:
* - Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.
* - Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.
* - Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.
* Thus all 5 bombs are detonated.
*
*
* Constraints:
*
* 1 <= bombs.length <= 100
* bombs[i].length == 3
* 1 <= xi, yi, ri <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/detonate-the-maximum-bombs/
// discuss: https://leetcode.com/problems/detonate-the-maximum-bombs/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_detonation(bombs: Vec<Vec<i32>>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2101_example_1() {
let bombs = vec![vec![2, 1, 3], vec![6, 1, 4]];
let result = 2;
assert_eq!(Solution::maximum_detonation(bombs), result);
}
#[test]
#[ignore]
fn test_2101_example_2() {
let bombs = vec![vec![1, 1, 5], vec![10, 10, 5]];
let result = 1;
assert_eq!(Solution::maximum_detonation(bombs), result);
}
#[test]
#[ignore]
fn test_2101_example_3() {
let bombs = vec![
vec![1, 2, 3],
vec![2, 3, 1],
vec![3, 4, 2],
vec![4, 5, 3],
vec![5, 6, 4],
];
let result = 5;
assert_eq!(Solution::maximum_detonation(bombs), result);
}
}
// Accepted solution for LeetCode #2101: Detonate the Maximum Bombs
function maximumDetonation(bombs: number[][]): number {
const n = bombs.length;
const g: number[][] = Array.from({ length: n }, () => []);
for (let i = 0; i < n - 1; ++i) {
const [x1, y1, r1] = bombs[i];
for (let j = i + 1; j < n; ++j) {
const [x2, y2, r2] = bombs[j];
const d = Math.hypot(x1 - x2, y1 - y2);
if (d <= r1) {
g[i].push(j);
}
if (d <= r2) {
g[j].push(i);
}
}
}
let ans = 0;
for (let k = 0; k < n; ++k) {
const vis: Set<number> = new Set([k]);
const q: number[] = [k];
for (const i of q) {
for (const j of g[i]) {
if (!vis.has(j)) {
vis.add(j);
q.push(j);
}
}
}
if (vis.size === n) {
return n;
}
ans = Math.max(ans, vis.size);
}
return ans;
}
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.