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.
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
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
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.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 4 8 9 3
3
1 2 4 8 9 2
8