


Input: 'n' = 3, 'k' = 2 and 'arr' = {1, 2, 3}
Output: 2
Explanation: The maximum possible minimum distance will be 2 when 2 cows are placed at positions {1, 3}. Here distance between cows is 2.
The first line contains two integers ‘n’ and ‘k’ denoting the number of elements in the array and the number of aggressive cows.
The second line contains ‘n’ single space-separated integers denoting the position of the stalls.
Return the largest possible minimum distance between cows.
You do not need to print anything; it has already been handled.
What we are looking for is that we need to place all the ‘k’ cows in the ‘n’ stalls such that the minimum distance between any two of them is as large as possible.
We need to define a check() function that checks if a distance ‘x’ is possible between each of the cows. We can use a greedy approach here by placing cows at the leftmost possible stalls such that they are at least 'x' distance away from the last-placed cow.
We need to sort the given array/list so once we have our sorted array, we can apply the whole array of the sorted input, and use our function check(x) to find the largest distance possible. And as soon as you reach a position from where it’s not possible to find a distance from which cows could be safe so we basically break the loop.
What we are looking for is that we need to place all the ‘k’ cows in the ‘n’ stalls such that the minimum distance between any two of them is as large as possible.
We need to define a check() function that checks if a distance 'x' is possible between each of the cows. We can use a greedy approach here by placing cows at the leftmost possible stalls such that they are at least ‘x’ distance away from the last-placed cow.
We need to sort the given array/list so once we have our sorted array, we can apply the Binary Search algorithm on the sorted input, and use our function check(x) to find the largest distance possible.
Steps are as follows :