Magnetic Force Between Two Balls

In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the i^th basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

Given the integer array position and the integer m, return the required force.

Example 1
Inputposition = [1,2,3,4,7], m = 3
Output3
Distributing the 3 balls into baskets 1, 4 and 7 makes the magnetic forces between ball pairs [3, 3, 6], so the minimum is 3 and no larger minimum can be achieved.
Example 2
Inputposition = [5,4,3,2,1,1000000000], m = 2
Output999999999
We can use baskets 1 and 1000000000.

Constraints

  • n == position.length
  • 2 <= n <= 10^5
  • 1 <= position[i] <= 10^9
  • All integers in position are distinct.
  • 2 <= m <= position.length

Asked at 10 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate