0
0
DSA Goprogramming

Aggressive Cows Maximum Minimum Distance in DSA Go

Choose your learning style9 modes available
Mental Model
We want to place cows in stalls so that the smallest distance between any two cows is as big as possible.
Analogy: Imagine placing friends in seats along a bench so that everyone has as much personal space as possible.
Stalls: [1] -> [2] -> [4] -> [8] -> [9]
Cows: C placed in some stalls
Goal: maximize minimum distance between any two C
Dry Run Walkthrough
Input: stalls: [1, 2, 4, 8, 9], cows: 3
Goal: Place 3 cows in stalls so that the smallest distance between any two cows is as large as possible
Step 1: Sort stalls and set search range low=1, high=8 (max distance between first and last stall)
Stalls: 1 -> 2 -> 4 -> 8 -> 9
Search range: low=1, high=8
Why: We need to search for the largest minimum distance between cows
Step 2: Check mid = (1+8)/2 = 4; try placing cows with at least 4 distance apart
Place cow at 1 (stall 1)
Next cow at stall ≥ 1+4=5 -> stall 8
Next cow at stall ≥ 8+4=12 -> no stall
Placed 2 cows < 3 needed
Why: Distance 4 too large, cannot place all cows
Step 3: Reduce high to mid-1 = 3; check mid = (1+3)/2 = 2; try placing cows with distance 2
Place cow at 1
Next cow at stall ≥ 3 -> stall 4
Next cow at stall ≥ 6 -> stall 8
Placed 3 cows successfully
Why: Distance 2 works, try for bigger distance
Step 4: Increase low to mid+1 = 3; check mid = (3+3)/2 = 3; try placing cows with distance 3
Place cow at 1
Next cow at stall ≥ 4 -> stall 4
Next cow at stall ≥ 7 -> stall 8
Placed 3 cows successfully
Why: Distance 3 works, try bigger distance
Step 5: Increase low to mid+1 = 4; now low=4, high=3 -> stop search
Final answer is high=3
Why: Search ends when low > high; largest minimum distance is 3
Result:
Largest minimum distance = 3
Annotated Code
DSA Go
package main

import (
	"fmt"
	"sort"
)

func canPlaceCows(stalls []int, cows int, dist int) bool {
	count := 1 // place first cow at first stall
	lastPos := stalls[0]
	for i := 1; i < len(stalls); i++ {
		if stalls[i]-lastPos >= dist {
			count++
			lastPos = stalls[i]
			if count == cows {
				return true
			}
		}
	}
	return false
}

func aggressiveCows(stalls []int, cows int) int {
	sort.Ints(stalls) // sort stalls
	low, high := 1, stalls[len(stalls)-1]-stalls[0]
	result := 0
	for low <= high {
		mid := (low + high) / 2
		if canPlaceCows(stalls, cows, mid) {
			result = mid // mid distance works
			low = mid + 1 // try bigger distance
		} else {
			high = mid - 1 // try smaller distance
		}
	}
	return result
}

func main() {
	stalls := []int{1, 2, 4, 8, 9}
	cows := 3
	maxDist := aggressiveCows(stalls, cows)
	fmt.Printf("Largest minimum distance = %d\n", maxDist)
}
sort.Ints(stalls) // sort stalls to check distances in order
sort stalls to check distances in order
low, high := 1, stalls[len(stalls)-1]-stalls[0]
set search range for minimum distance
mid := (low + high) / 2
choose middle distance to test
if canPlaceCows(stalls, cows, mid) {
check if cows can be placed with at least mid distance
result = mid // mid distance works
record current working distance
low = mid + 1 // try bigger distance
try to increase minimum distance
high = mid - 1 // try smaller distance
reduce distance if mid doesn't work
count := 1 // place first cow at first stall
start placing cows from first stall
if stalls[i]-lastPos >= dist {
place next cow only if distance condition met
if count == cows { return true }
all cows placed successfully
OutputSuccess
Largest minimum distance = 3
Complexity Analysis
Time: O(n log d) because we do binary search on distance (log d) and check placement in O(n)
Space: O(1) because we use only a few extra variables, no extra data structures
vs Alternative: Naive approach tries all distances linearly O(n*d), binary search reduces it to O(n log d)
Edge Cases
Only one stall
Can place only one cow, so max distance is 0
DSA Go
count := 1 // place first cow at first stall
Number of cows equals number of stalls
Cows must occupy all stalls, so minimum distance is minimum gap between adjacent stalls
DSA Go
if stalls[i]-lastPos >= dist {
All stalls at same position
Distance between any two stalls is zero, so max minimum distance is 0
DSA Go
high := stalls[len(stalls)-1]-stalls[0]
When to Use This Pattern
When asked to maximize minimum distance between placed items in sorted positions, use binary search on distance combined with a greedy check.
Common Mistakes
Mistake: Not sorting stalls before binary search
Fix: Always sort stalls first to check distances in order
Mistake: Checking placement incorrectly by not updating last placed position
Fix: Update lastPos only when a cow is placed to maintain correct distance checks
Summary
Find the largest minimum distance to place cows in stalls so no two cows are too close.
Use when you need to space out items evenly to maximize minimum gap.
Binary search on distance combined with greedy placement check is the key insight.