Challenge - 5 Problems
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of placing cows with minimum distance 3
Given the stalls at positions [1, 2, 8, 4, 9] and 3 cows, what is the maximum minimum distance between any two cows?
DSA C
int stalls[] = {1, 2, 8, 4, 9}; int n = 5; int cows = 3; // Function to check if cows can be placed with given distance int canPlaceCows(int dist) { int count = 1; int last_position = stalls[0]; for (int i = 1; i < n; i++) { if (stalls[i] - last_position >= dist) { count++; last_position = stalls[i]; if (count == cows) return 1; } } return 0; } // Binary search for max minimum distance int maxMinDist() { int low = 0, high = stalls[n-1] - stalls[0]; int result = 0; while (low <= high) { int mid = (low + high) / 2; if (canPlaceCows(mid)) { result = mid; low = mid + 1; } else { high = mid - 1; } } return result; } // Sort stalls before calling maxMinDist // After sorting: stalls = [1, 2, 4, 8, 9] // Output the result int main() { // Sorting code omitted for brevity int answer = maxMinDist(); printf("%d", answer); return 0; }
Attempts:
2 left
💡 Hint
Try placing cows starting from the first stall and check feasibility with binary search.
✗ Incorrect
After sorting stalls to [1, 2, 4, 8, 9], the maximum minimum distance to place 3 cows is 3. This is because cows can be placed at positions 1, 4, and 8 with distance 3 between them.
🧠 Conceptual
intermediateUnderstanding the feasibility check in Aggressive Cows problem
In the Aggressive Cows problem, what does the feasibility check function (like canPlaceCows) verify?
Attempts:
2 left
💡 Hint
Think about what the binary search is trying to find.
✗ Incorrect
The feasibility check ensures that cows can be placed in stalls so that the minimum distance between any two cows is at least the tested distance. This helps in binary searching the maximum minimum distance.
🔧 Debug
advancedIdentify the error in this feasibility check code snippet
What error will this code cause when checking feasibility for placing cows?
int canPlaceCows(int dist) {
int count = 0;
int last_position = stalls[0];
for (int i = 1; i < n; i++) {
if (stalls[i] - last_position >= dist) {
count++;
last_position = stalls[i];
}
}
return count >= cows;
}
Attempts:
2 left
💡 Hint
Consider how many cows are counted initially.
✗ Incorrect
The count should start at 1 because the first cow is placed at the first stall. Starting at 0 causes the function to underestimate the number of cows placed, leading to incorrect feasibility results.
❓ Predict Output
advancedOutput of maximum minimum distance for large input
Given sorted stalls at positions [1, 2, 4, 8, 9, 12, 16, 20] and 4 cows, what is the maximum minimum distance between any two cows?
DSA C
int stalls[] = {1, 2, 4, 8, 9, 12, 16, 20}; int n = 8; int cows = 4; // Assume canPlaceCows and maxMinDist functions as defined previously int main() { int answer = maxMinDist(); printf("%d", answer); return 0; }
Attempts:
2 left
💡 Hint
Try placing cows greedily with binary search on distance.
✗ Incorrect
The maximum minimum distance is 7, placing cows at positions 1, 8, 16, and 20. Distances are 7, 8, and 4, minimum is 4 but 7 is the largest minimum distance achievable.
🚀 Application
expertApplying Aggressive Cows logic to place WiFi routers
You have houses at positions [2, 5, 11, 15, 18, 22, 25] along a street. You want to place 3 WiFi routers to maximize the minimum distance between any two routers. What is the maximum minimum distance achievable?
Attempts:
2 left
💡 Hint
Sort the house positions and use binary search with feasibility check.
✗ Incorrect
The maximum minimum distance is 6, placing routers at positions 2, 11, and 18 or 2, 11, and 22. Distances between routers are at least 6.
