Ctrl K

Spread Out the Sensors

Medium
Along a pipeline there are mounting points whose coordinates are given in spots (all distinct, in no particular order). You must install count sensors, each at its own mounting point. Sensors interfere when they are close, so place them so that the smallest distance between any two sensors is as large as possible. The distance between points x and y is |x - y|. Return that largest possible smallest distance.
Example 1
spots=[12, 1, 6, 9, 3], count=3 → 5

Put the sensors at 1, 6 and 12: the gaps are 5 and 6, so the smallest is 5. Nothing better is possible.

Example 2
spots=[40, 10, 20, 30], count=4 → 10

Every spot is used, and neighbors are 10 apart.

Example 3
spots=[7, 500000000, 2], count=2 → 499999998

The two sensors go to the outermost spots, 2 and 500000000.

Constraints
  • 2 ≤ len(spots) ≤ 10⁵
  • 1 ≤ spots[i] ≤ 10⁹
  • All spots[i] are distinct
  • 2 ≤ count ≤ len(spots)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTriples Under a Limit MediumK-th Smallest in a Sorted Grid MediumNearest Following Slot MediumWarm Every Cabin MediumPairs With a Fixed Gap Medium
loading Python…
Input
spots=[12, 1, 6, 9, 3], count=3
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.