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, 8, 4, 9] and 3 cows, what is the maximum minimum distance between any two cows?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
let count = 1;
let last_position = stalls[0];
for (let i = 1; i < stalls.length; i++) {
if (stalls[i] - last_position >= dist) {
count++;
last_position = stalls[i];
if (count === cows) return true;
}
}
return false;
}
function aggressiveCows(stalls, cows) {
stalls.sort((a, b) => a - b);
let low = 0;
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, 8, 4, 9], 3));Attempts:
2 left
💡 Hint
Think about binary searching the minimum distance and checking feasibility.
✗ Incorrect
The maximum minimum distance that can be placed between 3 cows in stalls [1,2,4,8,9] is 3, but the binary search finds 5 as feasible and tries higher distances. The highest feasible minimum distance is 5, so the output is 5.
❓ Predict Output
intermediate2:00remaining
Output for stalls [10, 20, 30, 40, 50] and 2 cows
What is the maximum minimum distance between 2 cows placed in stalls at positions [10, 20, 30, 40, 50]?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
let count = 1;
let last_position = stalls[0];
for (let i = 1; i < stalls.length; i++) {
if (stalls[i] - last_position >= dist) {
count++;
last_position = stalls[i];
if (count === cows) return true;
}
}
return false;
}
function aggressiveCows(stalls, cows) {
stalls.sort((a, b) => a - b);
let low = 0;
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([10, 20, 30, 40, 50], 2));Attempts:
2 left
💡 Hint
With only 2 cows, place them at the farthest stalls.
✗ Incorrect
With 2 cows, placing them at the first and last stall gives the maximum minimum distance of 40 (50 - 10).
🔧 Debug
advanced2:00remaining
Identify the error in the canPlaceCows function
What error will the following code cause when running aggressiveCows([1, 2, 4, 8, 9], 3)?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
let count = 0;
let last_position = stalls[0];
for (let i = 1; i < stalls.length; i++) {
if (stalls[i] - last_position >= dist) {
count++;
last_position = stalls[i];
if (count === cows) return true;
}
}
return false;
}
function aggressiveCows(stalls, cows) {
stalls.sort((a, b) => a - b);
let low = 0;
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
Check the initial count of placed cows.
✗ Incorrect
The count should start at 1 because the first cow is placed at stalls[0]. Starting at 0 causes the function to underestimate cows placed and return false incorrectly.
🧠 Conceptual
advanced2:00remaining
Why use binary search in Aggressive Cows problem?
Why is binary search used to find the maximum minimum distance in the Aggressive Cows problem?
Attempts:
2 left
💡 Hint
Think about the range of possible distances and how to test them.
✗ Incorrect
Binary search is used on the range of possible distances because it is sorted and checking if cows can be placed at a given distance is efficient, allowing us to find the maximum feasible distance quickly.
🚀 Application
expert3:00remaining
Maximum minimum distance for large input
Given stalls at positions [1, 2, 4, 8, 9, 12, 15, 18, 20, 25] and 4 cows, what is the maximum minimum distance between any two cows?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
let count = 1;
let last_position = stalls[0];
for (let i = 1; i < stalls.length; i++) {
if (stalls[i] - last_position >= dist) {
count++;
last_position = stalls[i];
if (count === cows) return true;
}
}
return false;
}
function aggressiveCows(stalls, cows) {
stalls.sort((a, b) => a - b);
let low = 0;
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, 12, 15, 18, 20, 25], 4));Attempts:
2 left
💡 Hint
Use binary search and feasibility check to find the answer.
✗ Incorrect
The maximum minimum distance for placing 4 cows in the given stalls is 6. Distances like 7 or 8 are not feasible because cows cannot be placed with that spacing.