Complete the code to return the first element as minimum when the array is not rotated.
if nums[0] [1] nums[len(nums)-1] { return nums[0] }
If the first element is greater than the last, the array is rotated. Otherwise, the first element is the minimum.
Complete the code to calculate the middle index correctly.
mid := [1] + (high - low) / 2
To avoid overflow, middle index is calculated as low + (high - low) / 2.
Fix the error in the condition to decide which half to search next.
if nums[mid] [1] nums[high] { low = mid + 1 } else { high = mid }
If middle element is greater than the last element, minimum is in the right half.
Fill both blanks to complete the binary search loop and return statement.
for low [1] high { mid := low + (high - low) / 2 if nums[mid] > nums[high] { low = mid + 1 } else { high = mid } } return nums[2]
The loop runs while low is less than high. After the loop, nums[low] is the minimum.
Fill all three blanks to create a function that finds the minimum in a rotated sorted array.
func findMin(nums []int) int {
low, high := 0, len(nums) - 1
if nums[0] [1] nums[high] {
return nums[0]
}
for low [2] high {
mid := low + (high - low) / 2
if nums[mid] [3] nums[high] {
low = mid + 1
} else {
high = mid
}
}
return nums[low]
}The function checks if array is rotated, then uses binary search with correct comparisons to find minimum.