Binary Search Summary
Binary Search Template
-
Find First Position of Element is Sorted Array
-
Find Last Position of Element in Sorted Array
Binary Search to Find a Specific Position
- First Bad Version
- Find K Closest Elements
- Search in a Big Sorted Array (Exponential Backoff)
- Find minimum in a rotated Array
OOOO | X(minimum number)XXX
corner case: sorted array
= first or > last number? last, because if >= first, then you cannot find position to seperate sorted array. True: first position <= last number Wrong: first position <= or < first number Follow up: What if the sorted rotated array has repeated number? What is the worst case? Cannot solve in O(n) [1,1,1,…,0,1,1,1]
- Peak Index in a Mountain Array Follow up: Trinary Search
Half & Half
- Search in a Roatated Array only use binary search once