Aggressive Cows

medium

There are stalls scattered along a straight barn, and arr[i] gives the position of the i-th stall. All positions are distinct, but they are not necessarily given in sorted order.

You have k cows, and they are aggressive — put two of them close together and they fight. Assign every cow to a different stall so that the minimum distance between any two cows is as large as possible.

Return that largest possible minimum distance.

Hints

Constructing the best arrangement directly is hard. What if you flipped the question into a yes-no check: can all k cows be placed so every pair is at least d apart?
For a fixed d, a greedy left-to-right sweep over the sorted stalls answers that check in O(n): place a cow at the first stall at least d past the previous cow.
Feasibility is monotone — if d works, every smaller d works too. Binary search the largest feasible d between 1 and max(arr) - min(arr).

Common doubts

Shifting the first cow further left never hurts — it only leaves more room for every cow after it. So anchoring at the leftmost stall is always at least as good as any alternative, which makes the greedy check exact rather than a heuristic.
Yes. The stalls arrive in arbitrary order (see the second example), and both the greedy sweep and the search bound max(arr) - min(arr) assume positions increase left to right.
The answer is at least 1 — positions are distinct integers, so any k stalls are at least 1 apart — and at most max(arr) - min(arr), achieved only when k = 2 with cows at the two extreme stalls.
Extra placements past the k-th cow cannot lower the minimum gap of the first k, so once count == k the spacing d is proven feasible and the sweep can stop early.

Interview follow-ups

The greedy anchor at the leftmost stall no longer exists. Binary search on d still works, but the feasibility check must try anchoring at each stall within the first candidate gap (or fix one cow and sweep circularly), costing an extra factor over the linear version.
Same template with the predicate inverted: binary search the answer, and the greedy check counts how many groups, machines, or days a candidate value forces, comparing against the budget. Split Array Largest Sum and shipping-capacity problems work exactly this way.
Run the binary search to find the optimal d, then replay the greedy sweep once at that d and record the stalls where cows were placed — an extra O(n) pass.

Fun facts

  • Aggressive Cows is the poster child of the binary search on the answer pattern: instead of searching the array, you search the space of possible answers using a monotone yes-no predicate.
  • The same template with a swapped feasibility check solves Koko Eating Bananas, Smallest Divisor Given a Threshold, and Split Array Largest Sum — learn the checker, reuse the search.
  • The greedy proof here is a classic exchange argument: any valid arrangement can be shifted cow-by-cow to the leftmost feasible stalls without ever shrinking a gap.

Asked at

GoogleAmazonFlipkartMicrosoftUber
Frequently Sometimes Occasionally
Example 1
Input: arr = [1, 2, 4, 8, 9], k = 3
Output: 3
Place cows at stalls 1, 4, and 8. The gaps are 3 and 4, so the closest pair is 3 apart — no arrangement does better.
Example 2
Input: arr = [10, 1, 2, 7, 5], k = 3
Output: 4
Place cows at stalls 1, 5, and 10 (note the input is unsorted). The gaps are 4 and 5, so the closest pair is 4 apart — the maximum possible.
Constraints

- 2 <= arr.length <= 10^6 - 0 <= arr[i] <= 10^8 - 2 <= k <= arr.length - All stall positions are distinct

Solve this problem →