VOOZH about

URL: https://www.geeksforgeeks.org/dsa/assign-stalls-to-k-cows-to-maximize-the-minimum-distance-between-them/

⇱ Aggressive Cows - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Aggressive Cows

Last Updated : 12 Aug, 2025

Given an array stalls[] representing the positions of stalls and an integer k denoting the number of aggressive cows, place the cows in the stalls such that the minimum distance between any two cows is as large as possible. Return this maximum possible minimum distance.

Examples: 

Input: stalls[] = [1, 2, 4, 8, 9], k = 3
Output: 3
Explanation: We can place cow 1 at position 1, cow 2 at position 4 and cow 3 at position 9. So, the maximum possible minimum distance between two cows is 3.

Input: stalls[] = [6, 7,  9, 11, 13, 15], k = 4
Output: 2
Explanation: We can place cow 1 at position 6, cow 2 at position 9, cow 3 at position 11 and cow 4 at position 15. So, the maximum possible minimum distance between two cows is 2.

[Naive Approach] By iterating over all possible distances

The idea is to place k cows such that the minimum distance between any two is maximized. We sort the stalls and try all possible distances from 1 to the maximum gap between stalls.
For each distance, we check if cows can be placed by assigning the first cow to the first stall, and placing the next cow only if the gap from the last placed cow is at least that distance.
If placement is possible, we update our answer; the largest such distance is returned.


Output
3

Time Complexity: O(n × (max(stalls) - min(stalls))), where n is the size of the array, max(stalls) is the maximum element in the array and min(stalls) is minimum element in the array.
Auxiliary Space: O(1).

[Expected Approach] Using Binary Search on Answer

The minimum distance between cows follows a monotonic property, which makes binary search possible.

  • If we can place all cows with a minimum distance d, then placing them with any smaller distance is also possible because allowing smaller gaps gives us more flexibility and space to place cows closer together.
  • On the other hand, if we can’t place all cows with distance d, then any larger distance also won’t work—since increasing the gap makes placement even harder.

So, we apply binary search on the distance range to find the largest minimum distance possible.
To check if a distance works, we place the first cow at the first stall and place each next cow only if it’s at least d units away from the last one. If all cows fit, it’s a valid distance.


Output
3

Time Complexity: O(n log d), we first sort the stalls array, which takes O(n log n) time. After that, we perform a binary search on the distance range, from 1 to the maximum possible distance between the farthest stalls, which gives us log d steps (where d = stalls[n-1] - stalls[0] or max(stalls) - min(stalls) ). For each distance in the binary search, we run the check() function to verify if cow placement is possible, which takes O(n) time in the worst case.
Auxiliary Space: O(1)

Comment
Article Tags: