Binary Search · Pattern 4 of 8
Rotated sorted arrays
A sorted array cut and swapped. At every mid, one half is still sorted: find out which, then decide whether the answer can be in it.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- Sorted, then rotated by an unknown amount
- Find the minimum, the rotation count or a target in O(log n)
- Duplicates allowed (the harder version)
The template
int search(int[] a, int target) {
int lo = 0, hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) return mid;
if (a[lo] <= a[mid]) { // left half is sorted
if (a[lo] <= target && target < a[mid]) hi = mid - 1;
else lo = mid + 1;
} else { // right half is sorted
if (a[mid] < target && target <= a[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
// minimum: compare with the right end
int findMin(int[] a) {
int lo = 0, hi = a.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > a[hi]) lo = mid + 1; // the drop is right of mid
else hi = mid; // mid could be the minimum
}
return a[lo];
} def search(a, target):
lo, hi = 0, len(a) - 1
while lo <= hi:
mid = (lo + hi) // 2
if a[mid] == target:
return mid
if a[lo] <= a[mid]: # left half is sorted
if a[lo] <= target < a[mid]:
hi = mid - 1
else:
lo = mid + 1
else: # right half is sorted
if a[mid] < target <= a[hi]:
lo = mid + 1
else:
hi = mid - 1
return -1
def find_min(a): # compare with the right end
lo, hi = 0, len(a) - 1
while lo < hi:
mid = (lo + hi) // 2
if a[mid] > a[hi]:
lo = mid + 1 # the drop is right of mid
else:
hi = mid # mid could be the minimum
return a[lo] int search(const vector<int>& a, int target) {
int lo = 0, hi = (int)a.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) return mid;
if (a[lo] <= a[mid]) { // left half is sorted
if (a[lo] <= target && target < a[mid]) hi = mid - 1;
else lo = mid + 1;
} else { // right half is sorted
if (a[mid] < target && target <= a[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
// minimum: compare with the right end
int findMin(const vector<int>& a) {
int lo = 0, hi = (int)a.size() - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > a[hi]) lo = mid + 1; // the drop is right of mid
else hi = mid; // mid could be the minimum
}
return a[lo];
} function search(a, target) {
let lo = 0, hi = a.length - 1;
while (lo <= hi) {
const mid = (lo + hi) >>> 1;
if (a[mid] === target) return mid;
if (a[lo] <= a[mid]) { // left half is sorted
if (a[lo] <= target && target < a[mid]) hi = mid - 1;
else lo = mid + 1;
} else { // right half is sorted
if (a[mid] < target && target <= a[hi]) lo = mid + 1;
else hi = mid - 1;
}
}
return -1;
}
// minimum: compare with the right end
function findMin(a) {
let lo = 0, hi = a.length - 1;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (a[mid] > a[hi]) lo = mid + 1; // the drop is right of mid
else hi = mid; // mid could be the minimum
}
return a[lo];
} The variations
-
1Find the rotation point
The minimum is where the order breaks. If
a[mid] > a[hi]the break is to the right; otherwise mid is at or after it. Its index is also the rotation count.Now you solve -
2Search for a target
One half around mid is always sorted. If the target lies inside that sorted half's range go there, otherwise go to the other half.
-
3With duplicates
When the ends equal mid you cannot tell which half is sorted. Drop one end (
hi--) and carry on. Interviewers want you to say why this is O(n) in the worst case.Now you solve
Mistakes to avoid
- Using
<instead of<=ina[lo] <= a[mid]: with two elements left,lo == midand the left half is sorted. - Comparing
a[mid]witha[lo]to find the minimum: it fails when the array is not rotated. Compare witha[hi]. - With duplicates,
a[mid] == a[hi]tells you nothing: shrink withhi--(worst case O(n)).
Lesson done
Next: Searching a sorted 2D matrix →Mark it done once you can write the template from memory and you have solved the worked examples yourself.