Skip to content
Onsite

Aggressive Cows

MediumP2Coding

There are stalls.length stalls at the given positions on a line (not necessarily sorted) and cows cows to place, one per stall. Place the cows so that the minimum distance between any two of them is as large as possible.

Return that largest possible minimum distance.

Example 1

Input
stalls = [1,2,4,8,9], cows = 3
Output
3
Why
Place cows at 1, 4, 8 (or 1, 4, 9): gaps 3 and 4.

Example 2

Input
stalls = [1,2,3], cows = 2
Output
2

Example 3

Input
stalls = [5,5,5], cows = 2
Output
0

Constraints

  • 2 <= cows <= stalls.length <= 10^5
  • 0 <= stalls[i] <= 10^9

Hints

Run checks the 3 visible tests, Submit all 10.