📖 Scenario: You are helping a farmer place aggressive cows in stalls along a straight line. The farmer wants to place all cows so that the minimum distance between any two cows is as large as possible. You will write a program to find this largest minimum distance.
🎯 Goal: Build a Go program that takes stall positions and the number of cows, then calculates the maximum minimum distance possible between any two cows placed in those stalls.
📋 What You'll Learn
Create a slice called stalls with the exact values 1, 2, 4, 8, 9
Create an integer variable called cows and set it to 3
Write a function called canPlaceCows that checks if cows can be placed with at least a given distance
Use binary search to find the maximum minimum distance
Print the final maximum minimum distance
💡 Why This Matters
🌍 Real World
This problem models placing resources or devices in locations to maximize minimum spacing, useful in network design, agriculture, and facility planning.
💼 Career
Understanding binary search and greedy algorithms is essential for software engineering roles involving optimization and algorithmic problem solving.
Progress0 / 4 steps
1
Create the stalls slice
Create a slice called stalls with the exact values 1, 2, 4, 8, 9.
DSA Go
Hint
Use := to create and assign the slice stalls with the given numbers.
2
Add the cows variable
Create an integer variable called cows and set it to 3.
DSA Go
Hint
Use cows := 3 inside main to create the variable.
3
Write canPlaceCows function
Write a function called canPlaceCows that takes stalls []int, cows int, and distance int. It returns bool. The function should check if it is possible to place all cows in the stalls so that the minimum distance between any two cows is at least distance. Use a greedy approach starting from the first stall.
DSA Go
Hint
Start placing the first cow at the first stall. Then place next cows only if the distance condition is met. Return true if all cows placed, else false.
4
Find and print maximum minimum distance
Use binary search between low = 1 and high = stalls[len(stalls)-1] - stalls[0] to find the maximum minimum distance. Use canPlaceCows to check feasibility. Print the final maximum minimum distance using fmt.Println.
DSA Go
Hint
Use binary search on distance. If canPlaceCows returns true, try bigger distance. Else try smaller.
Practice
(1/5)
1. What is the main goal of the Aggressive Cows Maximum Minimum Distance problem?
easy
A. To arrange cows in increasing order of their stall numbers
B. To find the maximum number of cows that can fit in a single stall
C. To minimize the distance between all cows placed in the stalls
D. To place cows in stalls so that the minimum distance between any two cows is as large as possible
Solution
Step 1: Understand the problem objective
The problem asks to place cows in given stalls such that the smallest distance between any two cows is as large as possible.
Step 2: Clarify the goal
This means maximizing the minimum distance between cows, not minimizing or counting cows in one stall.
Final Answer:
To place cows in stalls so that the minimum distance between any two cows is as large as possible -> Option D
Quick Check:
Maximize minimum distance = Place cows far apart [OK]
Hint: Focus on maximizing the smallest gap between cows [OK]
Common Mistakes:
Thinking the goal is to minimize distance
Confusing number of cows with distance
Assuming cows can share stalls
2. Which of the following Go function signatures correctly represents the binary search helper function used in the Aggressive Cows problem?
easy
A. func canPlaceCows(stalls []int, dist int) bool
B. func canPlaceCows(cows int, dist int, stalls []int) int
C. func canPlaceCows(stalls []int, cows int, dist int) bool
D. func canPlaceCows(stalls []int, cows int) int
Solution
Step 1: Identify required parameters
The helper function needs stalls array, number of cows, and distance to check feasibility.
Step 2: Match correct signature
func canPlaceCows(stalls []int, cows int, dist int) bool has all parameters in correct order and returns bool indicating if placement is possible.
3. Given stalls at positions [1, 2, 8, 4, 9] and 3 cows, what is the maximum minimum distance between any two cows after running the Aggressive Cows algorithm?
4. Consider this Go snippet for checking if cows can be placed:
func canPlaceCows(stalls []int, cows int, dist int) bool {
count := 1
lastPos := stalls[0]
for i := 1; i < len(stalls); i++ {
if stalls[i]-lastPos >= dist {
count++
lastPos = stalls[i]
}
}
return count >= cows
}
What is the bug if stalls array is not sorted before calling this function?
medium
A. The function may incorrectly count placements because stalls are unordered
B. The function will cause a runtime panic due to index out of range
C. The function will always return false
D. No bug; sorting is not required
Solution
Step 1: Understand function logic
The function assumes stalls are sorted to place cows greedily at increasing positions.
Step 2: Effect of unsorted stalls
If stalls are unordered, distance checks become invalid, causing wrong count of cows placed.
Final Answer:
The function may incorrectly count placements because stalls are unordered -> Option A
Quick Check:
Unsorted stalls break greedy placement logic [OK]
Hint: Always sort stalls before placement check [OK]
Common Mistakes:
Ignoring sorting requirement
Assuming function panics without sorting
Thinking function always fails without sorting
5. You have stalls at positions [5, 10, 15, 20, 25, 30] and need to place 4 cows. If the minimum distance between cows must be at least 7, which is the correct maximum minimum distance achievable?
hard
A. 7
B. 5
C. 15
D. 10
Solution
Step 1: Analyze stall positions
Stalls: [5,10,15,20,25,30] (spaced by 5), 4 cows.
Step 2: Check dist=10
Greedy: place at 5 (last=5), skip 10 (5<10), place 15 (10>=10), skip 20 (5<10), place 25 (10>=10), skip 30 (5<10). 3 cows placed, fail.
Step 3: Check dist=7
Greedy: place at 5 (last=5), skip 10 (5<7), place 15 (10>=7), skip 20 (5<7), place 25 (10>=7), place 30 (5>=7? No, 5<7), so only 3 cows placed, fail.
Step 4: Check dist=6
Similar to above, 3 cows placed, fail.
Step 5: Check dist=5
Greedy: place at 5, 10, 15, 20 (all distances >=5), 4 cows placed, success.
Step 6: Final answer
Maximum minimum distance is 5, since 7 or more fails to place 4 cows.
Final Answer:
5 -> Option B
Quick Check:
Max min distance = 5 [OK]
Hint: Test distances with binary search and feasibility check [OK]
Common Mistakes:
Assuming max distance is stall range without checks
Ignoring minimum distance constraint
Misplacing cows ignoring stall positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.