Challenge - 5 Problems
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of maximum minimum distance calculation
What is the output of the following C# code that calculates the maximum minimum distance to place 3 cows in given stalls?
DSA C#
using System; using System.Linq; class Program { static bool CanPlaceCows(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]; if (count == cows) return true; } } return false; } static int MaxMinDistance(int[] stalls, int cows) { Array.Sort(stalls); int low = 0; int high = stalls[stalls.Length - 1] - stalls[0]; int result = 0; while (low <= high) { int mid = (low + high) / 2; if (CanPlaceCows(stalls, cows, mid)) { result = mid; low = mid + 1; } else { high = mid - 1; } } return result; } static void Main() { int[] stalls = {1, 2, 8, 4, 9}; int cows = 3; Console.WriteLine(MaxMinDistance(stalls, cows)); } }
Attempts:
2 left
💡 Hint
Think about placing cows with the largest minimum gap possible using binary search.
✗ Incorrect
The maximum minimum distance to place 3 cows in stalls at positions [1,2,4,8,9] is 3. This is found by binary searching the distance and checking feasibility.
🧠 Conceptual
intermediateUnderstanding the feasibility check in Aggressive Cows problem
In the Aggressive Cows problem, what does the feasibility function (like CanPlaceCows) check for a given distance?
Attempts:
2 left
💡 Hint
Focus on the minimum distance constraint between cows.
✗ Incorrect
The feasibility function tries to place cows in stalls such that the minimum distance between any two cows is at least the tested distance.
🔧 Debug
advancedIdentify the bug in the binary search implementation
What is the bug in this binary search code snippet for Aggressive Cows problem?
int low = 1;
int high = stalls[stalls.Length - 1] - stalls[0];
int result = -1;
while (low < high) {
int mid = (low + high) / 2;
if (CanPlaceCows(stalls, cows, mid)) {
result = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
Attempts:
2 left
💡 Hint
Consider what happens when low equals high.
✗ Incorrect
Using while (low < high) can miss checking the case when low equals high, potentially missing the correct maximum distance.
📝 Syntax
advancedIdentify the syntax error in this C# code snippet
What syntax error does this code produce?
bool CanPlaceCows(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];
if (count == cows) return true;
}
}
return false;
}
Attempts:
2 left
💡 Hint
Check line endings carefully.
✗ Incorrect
In C#, each statement must end with a semicolon. The line 'int count = 1' is missing a semicolon.
🚀 Application
expertMaximum minimum distance for large input
Given 5 stalls at positions [2, 5, 11, 15, 19] and 3 cows, what is the maximum minimum distance to place all cows?
Attempts:
2 left
💡 Hint
Try placing cows greedily and use binary search on distances.
✗ Incorrect
The maximum minimum distance is 8, placing cows at positions 2, 11, and 19.
