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 code that finds the largest minimum distance to place 3 cows in given stalls?
DSA Python
def can_place_cows(stalls, cows, dist): count = 1 last_position = stalls[0] for i in range(1, len(stalls)): if stalls[i] - last_position >= dist: count += 1 last_position = stalls[i] if count == cows: return True return False def aggressive_cows(stalls, cows): stalls.sort() low, high = 0, stalls[-1] - stalls[0] result = 0 while low <= high: mid = (low + high) // 2 if can_place_cows(stalls, cows, mid): result = mid low = mid + 1 else: high = mid - 1 return result stalls = [1, 2, 8, 4, 9] cows = 3 print(aggressive_cows(stalls, cows))
Attempts:
2 left
💡 Hint
Think about placing cows with the largest minimum gap possible.
✗ Incorrect
The largest minimum distance to place 3 cows in stalls [1,2,4,8,9] is 3. Cows can be placed at positions 1, 4, and 8.
🧠 Conceptual
intermediateUnderstanding the binary search range in Aggressive Cows
In the Aggressive Cows problem, what is the correct initial search range for the minimum distance between cows?
Attempts:
2 left
💡 Hint
Consider the smallest and largest possible distances between cows.
✗ Incorrect
The minimum distance can be as low as 0 and as high as the difference between the farthest stalls, so the search range is from 0 to max(stalls) - min(stalls).
🔧 Debug
advancedIdentify the error in the cow placement check function
What error will occur when running this code snippet for checking if cows can be placed with a given minimum distance?
DSA Python
def can_place_cows(stalls, cows, dist): count = 1 last_position = stalls[0] for i in range(1, len(stalls)): if stalls[i] - last_position > dist: count += 1 last_position = stalls[i] if count == cows: return True return False stalls = [1, 2, 4, 8, 9] cows = 3 print(can_place_cows(stalls, cows, 3))
Attempts:
2 left
💡 Hint
Check the comparison operator used for distance check.
✗ Incorrect
The condition uses '>' instead of '>=' which causes it to miss valid placements when distance equals dist, leading to incorrect False return.
❓ Predict Output
advancedOutput after placing 4 cows in stalls
What is the maximum minimum distance to place 4 cows in these stalls?
DSA Python
stalls = [5, 17, 100, 11] cows = 4 # Using the aggressive_cows function from previous examples def can_place_cows(stalls, cows, dist): count = 1 last_position = stalls[0] for i in range(1, len(stalls)): if stalls[i] - last_position >= dist: count += 1 last_position = stalls[i] if count == cows: return True return False def aggressive_cows(stalls, cows): stalls.sort() low, high = 0, stalls[-1] - stalls[0] result = 0 while low <= high: mid = (low + high) // 2 if can_place_cows(stalls, cows, mid): result = mid low = mid + 1 else: high = mid - 1 return result print(aggressive_cows(stalls, cows))
Attempts:
2 left
💡 Hint
Sort the stalls and try placing cows with different distances.
✗ Incorrect
After sorting stalls to [5, 11, 17, 100], the largest minimum distance to place 4 cows is 6 (positions 5, 11, 17, 100).
🧠 Conceptual
expertWhy binary search is suitable for Aggressive Cows problem?
Why is binary search an effective approach to solve the Aggressive Cows maximum minimum distance problem?
Attempts:
2 left
💡 Hint
Think about how checking feasibility changes with distance values.
✗ Incorrect
Binary search works because if cows can be placed at distance d, they can also be placed at any smaller distance. This monotonic property allows efficient search.
