Skip to content
Decoder

Magnetic Force Between Two Balls

Medium

Problem

There are baskets at the positions in position (not necessarily sorted). You must place m balls into distinct baskets so that the minimum distance between any two balls is as large as possible. Return that largest possible minimum distance.

Example 1

Input
position = [1, 2, 3, 4, 7], m = 3
Output
3
Why
balls at 1, 4, 7

Example 2

Input
position = [5, 4, 3, 2, 1, 1000000000], m = 2
Output
999999999

Example 3

Input
position = [1, 2], m = 2
Output
1

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.