MemotivaLeetCode Patterns Flashcards: Binary Search, Search Space, Templates, Edge Cases

How does binary search on a rotated sorted array work?

LeetCode Patterns Flashcards: Binary Search, Search Space, Templates, Edge Cases

Аудио-карточка · 0:29

Nortren·

How does binary search on a rotated sorted array work?

0:29

A rotated sorted array has two sorted halves. At each step, calculate the midpoint and determine which half is sorted by comparing the mid value with the leftmost value. If the left half is sorted and the target falls within its range, search left; otherwise search right. If the right half is sorted and the target falls within its range, search right; otherwise search left. The key insight is that at least one half is always properly sorted after rotation, and you can check if the target falls in the sorted half to make the correct elimination decision. This maintains O of log n time.
neetcode.io