Place k cows in stalls to maximize the minimum distance between any two

Quick Overview

Choose k stall positions on a line so that the smallest distance between any two placed cows is as large as possible. Tests binary search on the answer, a greedy feasibility check over sorted positions, and the argument for why feasibility is monotonic in the distance.

Place k cows in stalls to maximize the minimum distance between any two

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of integers `stalls` representing the positions of stalls along a straight line, and an integer `k` representing the number of cows to place, find the largest minimum distance between any two cows such that all `k` cows can be placed in the stalls. You must place exactly `k` cows in `k` distinct stalls. The goal is to maximize the minimum distance between any two of the placed cows. ### Function Signature ```python def max_min_distance(stalls: list[int], k: int) -> int: ``` ### Rules - `stalls` is not necessarily sorted. - Positions may repeat. Each stall holds at most one cow, and two cows in stalls at the same position are 0 apart. - The minimum distance of a placement is the smallest distance between any two of its cows. Return the largest such minimum over all ways to choose `k` stalls. ### Constraints - `2 <= len(stalls) <= 10^5` - `0 <= stalls[i] <= 10^9` - `2 <= k <= len(stalls)` ### Examples ```text Input: stalls = [1, 2, 8, 4, 9], k = 3 Output: 3 ``` After sorting, the stalls are `[1, 2, 4, 8, 9]`. Placing cows at 1, 4 and 9 gives pairwise distances 3, 5 and 8, so the minimum is 3. A minimum of 4 is impossible: starting at 1, the next stall at least 4 away is 8, and nothing is at least 4 beyond 8. ```text Input: stalls = [10, 1, 2, 7, 5], k = 3 Output: 4 ``` After sorting, the stalls are `[1, 2, 5, 7, 10]`. Placing cows at 1, 5 and 10 gives distances 4, 5 and 9, so the minimum is 4. No placement reaches 5. ```text Input: stalls = [4, 4, 9], k = 3 Output: 0 ``` All three stalls must be used, and two of them are at position 4, so two cows are 0 apart.

Overview: Choose k stall positions on a line so that the smallest distance between any two placed cows is as large as possible. Tests binary search on the answer, a greedy feasibility check over sorted positions, and the argument for why feasibility is monotonic in the distance.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers stalls representing the positions of stalls along a straight line, and an integer k representing the number of cows to place, find the largest minimum distance between any two cows such that all k cows can be placed in the stalls.

You must place exactly k cows in k distinct stalls. The goal is to maximize the minimum distance between any two of the placed cows.

Function Signature

def max_min_distance(stalls: list[int], k: int) -> int:

Rules

  • stalls is not necessarily sorted.
  • Positions may repeat. Each stall holds at most one cow, and two cows in stalls at the same position are 0 apart.
  • The minimum distance of a placement is the smallest distance between any two of its cows. Return the largest such minimum over all ways to choose k stalls.

Constraints

  • 2 <= len(stalls) <= 10^5
  • 0 <= stalls[i] <= 10^9
  • 2 <= k <= len(stalls)

Examples

Input: stalls = [1, 2, 8, 4, 9], k = 3
Output: 3

After sorting, the stalls are [1, 2, 4, 8, 9]. Placing cows at 1, 4 and 9 gives pairwise distances 3, 5 and 8, so the minimum is 3. A minimum of 4 is impossible: starting at 1, the next stall at least 4 away is 8, and nothing is at least 4 beyond 8.

Input: stalls = [10, 1, 2, 7, 5], k = 3
Output: 4

After sorting, the stalls are [1, 2, 5, 7, 10]. Placing cows at 1, 5 and 10 gives distances 4, 5 and 9, so the minimum is 4. No placement reaches 5.

Input: stalls = [4, 4, 9], k = 3
Output: 0

All three stalls must be used, and two of them are at position 4, so two cows are 0 apart.