Aggressive Cows Problem Statement

Given an array representing positions of stalls and an integer ‘K’ representing the number of aggressive cows, determine the largest minimum distance between any two cows when they are placed in the stalls such that they are as far apart as possible to prevent aggression.

Input:

The first line contains an integer ‘T’ representing the number of test cases.
For each test case:
The first line contains two integers, ‘N’ and ‘K’, where ‘N’ is the number of stalls, and ‘K’ is the number of cows.
The second line contains ‘N’ space-separated integers, each representing the position of a stall.

Output:

For each test case, output the largest minimum distance between any two cows placed in stalls.

Example:

Input:
T = 1
N = 5, K = 3
ARR = [1, 2, 8, 4, 9]
Output:
3

Constraints:

  • 1 <= T <= 5
  • 2 <= N <= 20000
  • 2 <= K <= N
  • 0 <= ARR[i] <= 10^9
  • Time Limit: 1 sec.
Note:
No need to print anything. The function should return the result for each test case.
AnswerBot
1y

The problem is to assign aggressive cows to stalls in a way that maximizes the minimum distance between any two cows.

  • Sort the array of stall positions in ascending order.

  • Use binary search to find the ...read more

Help your peers!
Select
Add answer anonymously...
ShareChat Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits