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, 4, 8, 9] and 3 cows, what is the maximum minimum distance between any two cows?
DSA Java
int[] stalls = {1, 2, 4, 8, 9}; int cows = 3; // Function call int result = maxMinDistance(stalls, cows); System.out.println(result);
Attempts:
2 left
💡 Hint
Try placing cows starting from the first stall and check if minimum distance 3 is possible.
✗ Incorrect
The maximum minimum distance is 3 because cows can be placed at positions 1, 4, and 8.
🧠 Conceptual
intermediateWhy sort the stalls before placing cows?
In the Aggressive Cows problem, why is it necessary to sort the stall positions before trying to place cows?
Attempts:
2 left
💡 Hint
Think about how distance between cows is calculated.
✗ Incorrect
Sorting helps to check stalls in order and calculate distances correctly for binary search.
🔧 Debug
advancedIdentify the error in this distance check function
What error does this code produce when checking if cows can be placed with given minimum distance?
boolean canPlace(int[] stalls, int cows, int dist) {
int count = 1;
int lastPos = stalls[0];
for (int i = 1; i < stalls.length; i++) {
if (stalls[i] - lastPos >= dist) {
count++;
lastPos = stalls[i];
}
}
return count >= cows;
}
Attempts:
2 left
💡 Hint
Check loop and conditions carefully.
✗ Incorrect
The function correctly counts cows placed with minimum distance and returns true if count meets cows.
❓ Predict Output
advancedOutput of maximum minimum distance for large input
Given stalls at positions [1, 2, 4, 8, 9, 12, 16, 20] and 4 cows, what is the maximum minimum distance?
DSA Java
int[] stalls = {1, 2, 4, 8, 9, 12, 16, 20}; int cows = 4; int result = maxMinDistance(stalls, cows); System.out.println(result);
Attempts:
2 left
💡 Hint
Try placing cows with distance 4 and check feasibility.
✗ Incorrect
Cows can be placed at 1, 8, 12, and 20 with minimum distance 6.
🚀 Application
expertMaximum minimum distance to place 5 cows in given stalls
You have stalls at positions [2, 5, 11, 15, 17, 20, 25] and need to place 5 cows. What is the maximum minimum distance between any two cows?
Attempts:
2 left
💡 Hint
Use binary search between 1 and max distance (23) to find feasible minimum distance.
✗ Incorrect
The maximum minimum distance is 5 by placing cows at 2, 5, 11, 17, 25.
