DSA Factory
Free Timed practice lessonsTimed practice · Stage 1 · Timed set 2 · Step 1

A chain reaction

Build a one-way graph from distances, then search from every starting point to find the biggest chain. About 14 minutes.

The graph is hidden in a rule

Each bomb has a position and a blast radius. When a bomb explodes, every bomb inside its radius explodes too. Which single bomb should you set off first to destroy the most bombs?

The graph isn't given, but it is easy to build. Bomb A has a one-way road to bomb B if B is inside A's blast. The roads are one-way because a large bomb may reach a small one without the small one reaching back. Then, for each starting bomb, count how many are reachable with a plain search.

Edge A to B: when B is inside A's blast.
One-way: A reaching B doesn't mean B reaches A.
Four bombs. Arrows show who triggers whom.
ABCD
exploded
1

Start with bomb A. Its blast reaches B.

Move 1 of 4
Quick check

Bomb A's blast reaches bomb B. Does that mean B's blast reaches A?

  1. ANot necessarily, because the radii can differ
  2. BYes, distance works both ways
  3. CYes, and so they form one group
Show the answer

Not necessarily, because the radii can differ. Distance is the same both ways, but each bomb has its own radius.

Your problem

Biggest chain reaction

bombs[i] = [x, y, r] describes a bomb at position (x, y) with blast radius r. When a bomb explodes, every other bomb whose position is within distance r of it (distance at most r) also explodes, and so on in a chain. Return the largest number of bombs that explode if you set off the best single bomb.

Example
bombs = [[2, 1, 3], [6, 1, 4]] → 2

1 ≤ bombs ≤ 100 · 0 ≤ x, y ≤ 100,000 · 1 ≤ r ≤ 100,000

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve