Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Aggressive Cows

Medium Acceptance 70% Points 30

You are given an array of integers stalls[] representing the positions of stalls on a number line and an integer k representing the number of cows. Your task is to place the cows in these stalls such that the minimum distance between any two cows is as large as possible. Each stall can hold only one cow. Return the maximum possible minimum distance between any two cows.

Examples
Example 1

Example 1

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

Output: 3

Explanation: The cows can be placed at positions 1, 4, and 8. The distances between cows are 3 and 4. The minimum distance is 3, which is the maximum possible.

Example 2

Example 2

Input: stalls = [1,3,5,7,9], k = 3

Output: 4

Explanation: The cows can be placed at positions 1, 5, and 9. The distances between cows are 4 and 4. The minimum distance is 4, which is the maximum possible.

Example 3

Example 3

Input: stalls = [10,20,30,40], k = 2

Output: 30

Explanation: The cows can be placed at positions 10 and 40. The distance between the cows is 30, which is the maximum possible minimum distance.

Constraints
  • 2 = stalls.length = 105
  • 1 = stalls[i] = 10?
  • 2 = k = stalls.length
Topics
Binary Search
Companies
Walmart
Added 19 Jul 2026 70% acceptance
Ln 1, Col 1Sign in to save your code
1 2 4 8 9
3
3