Complete the code to sort the stall positions.
stalls.sort_by(|a, b| a.[1](b));The sort_by method requires a comparator function like cmp to sort the vector.
Complete the code to check if cows can be placed with at least the given distance.
if stalls[i] - last_position >= [1] {
The variable distance represents the minimum required distance between cows.
Fix the error in the binary search loop condition.
while low [1] high {
The binary search loop should continue while low is less than or equal to high.
Fill both blanks to update the binary search bounds correctly.
if can_place_cows(&stalls, cows, mid) { result = mid; low = [1]; } else { high = [2]; }
If cows can be placed at distance mid, increase low to mid + 1 to try for a bigger distance. Otherwise, decrease high to mid - 1.
Fill all three blanks to complete the function signature and return statement.
fn can_place_cows(stalls: &Vec<i32>, cows: [1], distance: [2]) -> [3] { let mut count = 1; let mut last_position = stalls[0]; for i in 1..stalls.len() { if stalls[i] - last_position >= distance { count += 1; last_position = stalls[i]; if count == cows { return true; } } } false }
The number of cows is a usize (index type), distance is i32, and the function returns a bool indicating if placement is possible.
