Complete the code to return the first element of the array.
fn find_min(nums: &[i32]) -> i32 {
nums[[1]]
}The minimum element in a non-rotated sorted array is the first element at index 0.
Complete the code to find the middle index between left and right.
let mid = (left [1] right) / 2;
To find the middle index, add left and right, then divide by 2.
Fix the error in the comparison to decide which half to search next.
if nums[mid] [1] nums[right] { left = mid + 1; } else { right = mid; }
If nums[mid] is greater than nums[right], the minimum is in the right half, so move left to mid + 1.
Fill both blanks to complete the while loop condition and return statement.
while [1] < [2] { let mid = (left + right) / 2; if nums[mid] > nums[right] { left = mid + 1; } else { right = mid; } } return nums[left];
The loop continues while left is less than right. After the loop, nums[left] is the minimum.
Fill all three blanks to complete the function that finds the minimum in a rotated sorted array.
fn find_min(nums: &[i32]) -> i32 {
let (mut left, mut right) = (0, nums.len() [1] 1);
while left [2] right {
let mid = (left + right) / 2;
if nums[mid] [3] nums[right] {
left = mid + 1;
} else {
right = mid;
}
}
nums[left]
}Initialize right as length minus one. Loop while left is less than right. If nums[mid] is greater than nums[right], move left to mid + 1.
