Magnetic Force Between Two Balls

In a universe, Rick has n empty baskets, where the i-th basket is at position[i], and Morty has m balls. Rick wants to distribute the balls into the baskets so that the minimum magnetic force between any two balls is maximum. The magnetic force between two balls at positions x and y is |x - y|. Given the integer array position and the integer m, return the required maximum of the minimum force.

30:00

Magnetic Force Between Two Balls
medium
Free written solution
Topics
Companies

In a universe, Rick has n empty baskets, where the i-th basket is at position[i], and Morty has m balls. Rick wants to distribute the balls into the baskets so that the minimum magnetic force between any two balls is maximum. The magnetic force between two balls at positions x and y is |x - y|. Given the integer array position and the integer m, return the required maximum of the minimum force.

Example 1:
Input: {"position":[1,2,3,4,7],"m":3}
Output: 3
Constraints:
  • n==position.lengthn == \text{position.length}

  • 2≤n≤1052 \leq n \leq 10^5

  • 1≤position[i]≤1091 \leq \text{position}[i] \leq 10^9

  • All integers in position are distinct.

  • 2≤m≤position.length2 \leq m \leq \text{position.length}

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"position":[1,2,3,4,7],"m":3}

Place 3 balls in 5 baskets so the closest pair of balls is as far apart as possible. Start by sorting the basket positions.

Baskets, as given

balls placed 0 / 3

1
2
3
4
7
Variables
VariableValue
n5
m3
DepthFunction Call
Stack empty
0/15