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!
Add answer anonymously...
ShareChat Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter