Detonate the Maximum Bombs - Problem

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.

Input & Output

Example 1 — Chain Reaction
$ Input: bombs = [[2,1,3],[6,1,4]]
Output: 2
💡 Note: Starting with bomb 0 at (2,1) with radius 3: it cannot reach bomb 1 at (6,1) since distance = 4 > radius 3. Starting with bomb 1 at (6,1) with radius 4: it can reach bomb 0 at (2,1) since distance = 4 ≤ radius 4. So maximum is 2 bombs when starting with bomb 1.
Example 2 — No Chain Reaction
$ Input: bombs = [[1,1,5],[10,10,5]]
Output: 1
💡 Note: The two bombs are too far apart. Distance between (1,1) and (10,10) is √((10-1)² + (10-1)²) = √162 ≈ 12.7, which is greater than either bomb's radius of 5. No chain reaction possible, so maximum is 1.
Example 3 — Multiple Chain Reactions
$ Input: bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
Output: 5
💡 Note: Starting from the right bomb and working backwards, we can create a chain reaction that detonates all 5 bombs due to their overlapping ranges.

Constraints

  • 1 ≤ bombs.length ≤ 100
  • bombs[i].length == 3
  • 1 ≤ xi, yi, ri ≤ 105

Visualization

Tap to expand
Detonate the Maximum Bombs INPUT X Y B0 B1 (2,1) r=3 (6,1) r=4 bombs = [[2,1,3],[6,1,4]] 2 bombs with positions and blast radii ALGORITHM STEPS 1 Build Graph Check if bomb i can reach j dist(i,j) <= radius[i] 2 Create Edges B0-->B1: dist=4, r=3 (NO) B1-->B0: dist=4, r=4 (YES) B0 B1 can reach 3 DFS from each bomb Start B0: reaches 1 bomb Start B1: reaches 2 bombs 4 Track Maximum max(1, 2) = 2 FINAL RESULT B1 B0 Detonate B1 first Chain reaction to B0 Output 2 OK - Maximum! Both bombs detonated by starting at B1 Key Insight: This is a directed graph problem! Bomb A reaching B doesn't mean B can reach A. Build a directed graph where edge A-->B exists if distance(A,B) <= radius(A). Run DFS from each node and track the maximum reachable nodes. Time: O(n^2), Space: O(n^2) TutorialsPoint - Detonate the Maximum Bombs | DFS with Graph Construction
Asked in
Google 8 Amazon 5 Meta 3 Apple 2
32.2K Views
Medium Frequency
~25 min Avg. Time
847 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