Given an array of length ‘N’, where each element denotes the position of a stall. Now you have ‘N’ stalls and an integer ‘K’ which denotes the number of cows that are aggressive. To prevent the cows from hurting each other, you need to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. Return the largest minimum distance.
Input format :
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains two integers ‘N’ and ‘K’ denoting the number of elements in the array/list and the number of aggressive cows.
The second line contains ‘N’ single space-separated integers denoting the elements of the array.
Output format :
For each test case, print the majority element in a separate line.
Note :
You do not need to print anything; it has already been taken care of.
Constraints :
1 <= T <= 5
2 <= N <= 20000
2 <= C <= N
0 <= ARR[i] <= 10 ^ 9
Where ‘T’ is the number of test cases, 'N' is the length of the given array and, ARR[i] denotes the i-th element in the array.
Time Limit: 1 sec.
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
Step 1 -> Gave a brute force solution.
Step 2 -> Interviewer asked me to optimize it.
Step 3 -> Optimized it using Binary Search.
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()...read more
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()...read more
Top ShareChat Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month