Aggressive Cows Problem GFG
You are given an array with unique elements of stalls[], which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. Your task is to assign stalls to k cows such that the minimum distance between any two of them is the maximum possible.
Examples :
Input: stalls[] = [1, 2, 4, 8, 9], k = 3
Output: 3
Explanation: The first cow can be placed at stalls[0],
the second cow can be placed at stalls[2] and
the third cow can be placed at stalls[3].
The minimum distance between cows, in this case, is 3, which also is the largest among all possible ways.
Input: stalls[] = [10, 1, 2, 7, 5], k = 3
Output: 4
Explanation: The first cow can be placed at stalls[0],
the second cow can be placed at stalls[1] and
the third cow can be placed at stalls[4].
The minimum distance between cows, in this case, is 4, which also is the largest among all possible ways.
Input: stalls[] = [2, 12, 11, 3, 26, 7], k = 5
Output: 1
Explanation: Each cow can be placed in any of the stalls, as the no. of stalls are exactly equal to the number of cows.
The minimum distance between cows, in this case, is 1, which also is the largest among all possible ways.
// { Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) throws IOException {
// Input reader setup
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Read number of test cases
int tc = Integer.parseInt(br.readLine().trim());
// Run each test case
while (tc-- > 0) {
// Read the stall positions as a space-separated string and convert to int array
String[] str = br.readLine().trim().split(" ");
int[] a = new int[str.length];
for (int i = 0; i < str.length; i++) {
a[i] = Integer.parseInt(str[i]);
}
// Read number of cows to place
String[] nk = br.readLine().trim().split(" ");
int k = Integer.parseInt(nk[0]);
// Call the solution method
Solution sln = new Solution();
int ans = sln.aggressiveCows(a, k);
// Print result for each test case
System.out.println(ans);
System.out.println("~");
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solution {
public static int aggressiveCows(int[] stalls, int k) {
// Step 1: Sort the stall positions
Arrays.sort(stalls);
int n = stalls.length;
// Step 2: Binary search for the largest minimum distance
int start = 1; // Smallest possible distance between cows
int end = stalls[n - 1] - stalls[0]; // Maximum distance between farthest stalls
int ans = 0;
// Step 3: Binary search loop
while (start <= end) {
int mid = start + (end - start) / 2; // Try this distance as the minimum
int cowcount = 1; // Place the first cow in the first stall
int cowposition = stalls[0]; // Last placed cow's position
// Step 4: Try placing the rest of the cows
for (int i = 1; i < n; i++) {
// If the current stall is at least `mid` distance away from last cow
if (stalls[i] >= cowposition + mid) {
cowcount++; // Place a new cow
cowposition = stalls[i]; // Update last cow's position
}
}
// Step 5: Check if all cows can be placed with at least `mid` distance
if (cowcount < k) {
// Couldn't place all cows → try smaller distance
end = mid - 1;
} else {
// Cows placed successfully → try for a bigger distance
ans = mid;
start = mid + 1;
}
}
// Step 6: Return the best found distance
return ans;
}
}
Comments
Post a Comment