Loading…
Loading…
Charting your lesson…
Opening your lesson…
Search in Rotated and 2D Arrays
Lesson 7 of 7
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Two binary-search variants show up constantly: searching a rotated sorted array and searching a sorted 2-D matrix. Both keep the O(log n) magic by cleverly deciding which half (or which direction) to discard.
Core idea: binary search works whenever you can decide, in O(1), which side of the middle to keep. A rotated array still has one sorted half at every step; a sorted matrix can be treated as one long sorted list or navigated from a corner.
Step 2/ 61 min
A rotated sorted array (e.g. [4,5,6,7,0,1,2]) has a pivot, but at any mid one half is fully sorted. Check which, then decide if the target lies in it:
while lo <= hi:
mid = (lo + hi)//2
if a[mid] == target: return mid
if a[lo] <= a[mid]: # left half sorted
if a[lo] <= target < a[mid]: hi = mid-1
else: lo = mid+1
else: # right half sorted
if a[mid] < target <= a[hi]: lo = mid+1
else: hi = mid-1
O(log n) — still logarithmic despite the rotation.
index → (index // n, index % n) → O(log(m·n)).[2,2,2,0,2]), you can't always tell which half is sorted when a[lo] == a[mid]; shrink lo++ in that case, degrading to O(n) worst case.a[mid] > a[hi] (minimum is to the right) — useful for "find min in rotated array".Step 3/ 61 min
Search 0 in [4,5,6,7,0,1,2]. lo=0,hi=6,mid=3 → a[3]=7≠0. Left half a[0..3]=[4,5,6,7] sorted; is 0 in [4,7)? No → search right, lo=4. mid=5 → a[5]=1≠0; left half a[4..5]=[0,1] sorted; is 0 in [0,1)? Yes → hi=4. mid=4 → a[4]=0 → return index 4. O(log n) even with the rotation.
Step 4/ 61 min
Step 5/ 61 min
a[lo] <= a[mid]), then test if the target lies within that sorted half's range.Using the O(m+n) top-right corner walk on a fully sorted matrix and calling it optimal, or applying O(log(m·n)) index-mapping to a matrix that is only row-and-column sorted (where rows don't chain). Match the algorithm to the matrix's exact sortedness guarantee.
Step 6/ 6· Worked examples1 min
Problem
Find the index of 0 in the rotated sorted array [4, 5, 6, 7, 0, 1, 2] using modified binary search.
Try it on paper first.
Recall
Keep this
Binary search extends to trickier sorted structures: a rotated sorted array (sorted then shifted) and a 2D matrix with sorted rows/columns. Both keep the O(log n)-style efficiency, but require an extra observation about which half or direction is ordered.
| Structure | Method | Time |
|---|---|---|
| Rotated sorted array | find sorted half | O(log n) |
| Rotated with duplicates | may need linear scan | O(n) worst |
| Fully sorted matrix | flatten + binary search | O(log(m·n)) |
| Row/col-sorted matrix | top-right staircase | O(m + n) |
Search in a rotated sorted array (with/without duplicates), find minimum in a rotated array, search a 2D matrix (both variants), or Kth smallest in a sorted matrix. Interviewers check the "which half is sorted" logic and the two matrix models.
Rotated array search finds the sorted half and checks the target against it — O(log n) (O(n) with duplicates). Fully sorted matrix → flatten and binary search O(log(m·n)); row/col-sorted matrix → top-right staircase O(m + n).
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.