Magnetic Force Between Two Balls

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Magnetic Force Between Two Balls

Approach

Sort basket positions, then binary search the minimal spacing candidate. A greedy pass checks whether we can place m balls with at least that spacing.

Complexity Analysis

Time
O(n log R)
Space
O(1)

Pattern

Binary Search on Answer

Why It Works

If a spacing d is feasible, any smaller spacing is also feasible. So the optimum is found by binary searching the highest feasible spacing.

Updated Feb 2026