Magnetic Force Between Two Balls - Problem

The Magnetic Ball Distribution Challenge

In Rick's laboratory, he has discovered a unique property of magnetic force between balls placed in special baskets. Given n baskets positioned at different locations and m magnetic balls, your task is to distribute the balls optimally.

The Goal: Place m balls into m different baskets such that the minimum magnetic force between any two balls is maximized.

Magnetic Force Formula: The magnetic force between two balls at positions x and y is |x - y| (absolute difference).

Input: An integer array position representing basket locations and integer m representing number of balls.

Output: Return the maximum possible minimum magnetic force.

Example: If baskets are at positions [1, 2, 3, 4, 7] and you have 3 balls, placing them at positions [1, 4, 7] gives minimum force of 3 (distances: 3, 3, 6).

Input & Output

example_1.py โ€” Basic Case
$ Input: position = [1,2,3,4,7], m = 3
โ€บ Output: 3
๐Ÿ’ก Note: We can place balls at positions [1,4,7] giving minimum magnetic forces of |4-1|=3 and |7-4|=3. This is the maximum possible minimum force.
example_2.py โ€” Minimum Balls
$ Input: position = [5,4,3,2,1,1000000000], m = 2
โ€บ Output: 999999999
๐Ÿ’ก Note: With only 2 balls, we can place them at positions 1 and 1000000000 for maximum magnetic force of 999999999.
example_3.py โ€” All Positions Used
$ Input: position = [1,2,3,4,5], m = 5
โ€บ Output: 1
๐Ÿ’ก Note: We must use all positions, so the minimum force is limited by the smallest gap, which is 1.

Constraints

  • 2 โ‰ค n โ‰ค 105
  • 2 โ‰ค m โ‰ค n
  • 1 โ‰ค position[i] โ‰ค 109
  • All positions are distinct
  • m balls must be placed in m different baskets

Visualization

Tap to expand
Security Guard Placement Strategy12347๐Ÿ›ก๏ธ๐Ÿ›ก๏ธ๐Ÿ›ก๏ธdistance = 3distance = 3Binary Search ProcessStep 1: Can we achieve minimum distance = 3?Greedy check: Place guard at position 1Next position โ‰ฅ 1 + 3 = 4 โ†’ Place at position 4Next position โ‰ฅ 4 + 3 = 7 โ†’ Place at position 7โœ“ Success! 3 guards placed with minimum distance 3Step 2: Can we achieve minimum distance = 4?Greedy check: Place guard at position 1Next position โ‰ฅ 1 + 4 = 5 โ†’ Place at position 7โœ— Failed! Only 2 guards placed, need 3๐ŸŽฏ Result: Maximum minimum distance = 3
Understanding the Visualization
1
Sort Building Positions
Arrange buildings in order: [1, 2, 3, 4, 7]
2
Binary Search on Distance
Test if minimum distance of 3 units is achievable
3
Greedy Placement
Place first guard at position 1, next at position 4 (โ‰ฅ1+3), last at position 7 (โ‰ฅ4+3)
4
Verify Success
Successfully placed 3 guards with minimum distance 3
Key Takeaway
๐ŸŽฏ Key Insight: Instead of trying all combinations, we binary search on the answer (minimum distance) and use a greedy algorithm to verify if each distance is achievable. This reduces complexity from exponential to O(n log n).
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22 Apple 18
67.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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