Dsa
Greedy
Hard
Maximize Minimized Distance
You are given a list of positions representing stalls and an integer k representing the number of cows. Your task is to place k cows in these stalls such that the minimum distance between any two cows is maximized. Write a program that reads the number of stalls, the list of positions, and the number of cows from STDIN, and prints the largest minimum distance to STDOUT.
Input format: First line contains an integer n (1 <= n <= 10^5), the number of stalls. Second line contains n integers (0 <= positions[i] <= 10^9) representing the positions of the stalls, sorted in non-decreasing order. Third line contains an integer k (1 <= k <= n), the number of cows.
Output format: A single integer, the largest minimum distance between any two cows when placed in the stalls.
Example:
Input:
5
1 2 8 4 9
3
Output:
3
Key concepts
greedysortingbinary search
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now