Challenge - 5 Problems
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 Typescript
function canPlaceCows(stalls: number[], cows: number, dist: number): boolean {
let count = 1;
let lastPos = stalls[0];
for (let i = 1; i < stalls.length; i++) {
if (stalls[i] - lastPos >= dist) {
count++;
lastPos = stalls[i];
if (count === cows) return true;
}
}
return false;
}
function aggressiveCows(stalls: number[], cows: number): number {
stalls.sort((a, b) => a - b);
let low = 1;
let high = stalls[stalls.length - 1] - stalls[0];
let result = 0;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (canPlaceCows(stalls, cows, mid)) {
result = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
console.log(aggressiveCows([1, 2, 4, 8, 9], 3));Attempts:
2 left
💡 Hint
Try placing cows starting with the smallest distance and increase it using binary search.
✗ Incorrect
The maximum minimum distance to place 3 cows in stalls [1,2,4,8,9] is 3. This is found by binary searching the distance and checking feasibility.
🧠 Conceptual
intermediate1:30remaining
Why do we sort stalls before placing cows?
In the Aggressive Cows problem, why is it necessary to sort the stall positions before applying the binary search approach?
Attempts:
2 left
💡 Hint
Think about how distance checking works between stalls.
✗ Incorrect
Sorting stalls allows us to check distances between adjacent stalls in order and place cows greedily from left to right.
🔧 Debug
advanced1:30remaining
Identify the error in this code snippet
What error will this TypeScript code produce when trying to find the maximum minimum distance for aggressive cows?
function canPlaceCows(stalls: number[], cows: number, dist: number): boolean {
let count = 1;
let lastPos = stalls[0];
for (let i = 1; i < stalls.length; i++) {
if (stalls[i] - lastPos > dist) {
count++;
lastPos = stalls[i];
if (count === cows) return true;
}
}
return false;
}
// rest of code omitted for brevity
Attempts:
2 left
💡 Hint
Check the comparison operator in the if condition.
✗ Incorrect
The condition should be '>=' to allow placing cows at exactly the minimum distance, otherwise some valid placements are missed.
🚀 Application
advanced1:30remaining
Maximum minimum distance for large input
Given 5 stalls at positions [10, 20, 30, 40, 50] and 3 cows, what is the maximum minimum distance to place all cows?
Attempts:
2 left
💡 Hint
Try placing cows with distances 10, 15, 20 and check feasibility.
✗ Incorrect
The maximum minimum distance is 20 because cows can be placed at 10, 30, and 50 with minimum distance 20. It is not possible with distance 25 or more.
🧠 Conceptual
expert2:00remaining
Why binary search works on distance, not positions?
In the Aggressive Cows problem, why do we apply binary search on the minimum distance between cows instead of the stall positions themselves?
Attempts:
2 left
💡 Hint
Think about what the problem is asking to maximize and how binary search helps.
✗ Incorrect
Binary search on distance helps find the largest minimum gap that can be maintained between cows, which is the problem's goal.