Menu
CoddyTech

Aggressive Cows

A barn has stalls along one straight line, and stalls[i] is the position of stall i. The positions are all different and come in no particular order. You must put k cows into k different stalls, and the cows fight when they stand close, so you want the two closest cows as far apart as you can get them.

Return the largest possible value of the smallest distance between any two cows.

Function

aggressiveCows(stalls: integer-array, k: integer) → integer
stallsinteger-array
the distinct stall positions, in any order
kinteger
the number of cows to place
Returnsinteger
the largest possible smallest distance between two cows

Constraints

  • 2 ≤ stalls.length ≤ 5000
  • 0 ≤ stalls[i] ≤ 109
  • All positions in stalls are distinct.
  • 2 ≤ k ≤ stalls.length

Examples

Input
stalls = [10, 1, 7, 4, 15]k = 3
Output
6
Explanation
Cows at 1, 7 and 15 stand 6 and 8 apart. A gap of 7 fails: after 1 the next cow goes to 10, and no stall is at 17 or beyond.
challenge icon

Follow-up

What changes if the stalls stand on a circle, so the last stall in order is also a neighbour of the first?

Reset code
def aggressiveCows(stalls, k):
    # Write code here

Test #1

Test #2

Test #3

Arguments

stalls = [10, 1, 7, 4, 15]
k = 3

Expected Output

6