Magnetic Force Between Two Balls

medium

Place m balls in baskets to maximize minimum distance

Magnetic Force Between Two Balls

Key Insight

Binary search minimum distance and greedily place balls whenever spacing is large enough.

Step 1Sort and Search
Greedy placement check
L
1
0
2
1
3
2
4
3
7
4
Search space: [1..6]

Positions [1, 2, 3, 4, 7], m=3. Search distance from 1..6.

1 / 3

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Magnetic Force Between Two Balls

Binary search minimum distance and greedily place balls whenever spacing is large enough.

  1. Sort and Search

    Positions [1, 2, 3, 4, 7], m=3. Search distance from 1..6.

  2. Try mid=3

    Place at 1 and 4 gives distance 3; place next at 7 gives distance 3. Feasible.

  3. Try larger distance

    Move to higher candidate. Distance 4 still feasible, then narrow.