Find pivot element in a sorted array
Pattern: Binary search on rotated arrays
In a rotated sorted array the pivot is the largest value, the top of the one fall. It sits just before the minimum, so find the minimum's index and step one back, wrapping round to the end when the array was not rotated.
The problem
You are given a sorted array of different whole numbers that was rotated at an unknown point. Return the index of its
pivot: the largest value, which is where the rotation happened. For example, [4, 5, 6, 7, 0, 1, 2] gives 3 and
[3, 4, 5, 1, 2] gives 2.
The link above is a blog article on this topic. This guide's function name findPivot and its exact rule are its own:
the pivot is the index of the largest value, and an array that was not rotated returns its last index. Articles differ
on these details, so check the rule before you compare answers.
Think first
- Where is the largest value relative to the smallest one?
- You already know how to find the smallest value's index in O(log n). What is left?
- What if the array was not rotated at all?
Hints
Brute force, and why it fails
Scan for the largest value: O(n). Or scan for the one index with nums[i] > nums[i + 1]. Binary search is O(log n).
The key insight
All three rotation questions, the smallest value, the rotation count and the pivot, come from one search for the fall. Once you have the minimum's index, the others are one step away.
Dry run
nums = [4, 5, 6, 7, 0, 1, 2]. The pivot is the 7, the last value before the fall.
The plan
lo = 0, hi = n - 1
while lo < hi:
mid = lo + (hi - lo) / 2
if nums[mid] > nums[hi]: lo = mid + 1
else: hi = mid
return (lo - 1 + n) mod n
Complexity
- Time
O(log n)- Space
O(1)
One Find Minimum search plus O(1) work.
Edge cases
- Not rotated: the last index.
- One value: index 0.
- Two values,
[2, 1]: index 0.
I've built my logic, show the code
Java
class Solution {
public int findPivot(int[] nums) {
int n = nums.length;
int lo = 0, hi = n - 1; // first find the index of the minimum
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
}
return (lo - 1 + n) % n; // the largest value sits just before the minimum (wrapping round)
}
}C++
class Solution {
public:
int findPivot(vector<int>& nums) {
int n = static_cast<int>(nums.size());
int lo = 0, hi = n - 1; // first find the index of the minimum
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] > nums[hi]) lo = mid + 1;
else hi = mid;
}
return (lo - 1 + n) % n; // the largest value sits just before the minimum (wrapping round)
}
};Python
class Solution:
def findPivot(self, nums: list[int]) -> int:
n = len(nums)
lo, hi = 0, n - 1 # first find the index of the minimum
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = mid
return (lo - 1 + n) % n # the largest value sits just before the minimum (wrapping round)Common mistakes
- Writing
lo - 1without wrapping, which gives -1 for an array that was not rotated. - Searching for the maximum directly with
nums[mid] < nums[lo], which is easy to get wrong at the ends. - Returning the value instead of the index.