I knew binary search was involved but my first instinct was to just scan linearly, which obviously fails the complexity requirement.
Use a modified binary search that compares the middle element with its neighbors to determine which half contains a local minimum. At each step, move toward the smaller neighbor, guaranteeing a local minimum exists in that direction. This achieves O(log n) time by halving the search space each iteration.
Pro tip: Explicitly state the invariant that the subarray being searched always contains at least one local minimum, and handle edge cases (e.g., when mid is at the boundary) by treating out-of-bounds as positive infinity. This shows rigorous reasoning and preempts off-by-one errors.
Confirm that the array has no two adjacent equal elements, and that out-of-bounds positions are treated as positive infinity. This ensures a local minimum always exists.
Maintain that the current search interval [low, high] always contains at least one local minimum. Initially, the whole array satisfies this because the global minimum is a local minimum.
Compute mid = (low + high) / 2. Compare arr[mid] with arr[mid-1] (or +inf if mid=0) and arr[mid+1] (or +inf if mid=n-1). If arr[mid] is smaller than both, return mid.
If arr[mid] > arr[mid-1], then a local minimum must exist in the left half (low to mid-1). Otherwise, if arr[mid] > arr[mid+1], search the right half (mid+1 to high). Update low or high accordingly.
Continue the binary search until a local minimum is found. The loop runs in O(log n) time because the interval size halves each iteration.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.