Skip to main content

Rotated & Mountain

The array is sorted in pieces: two ascending runs, or up then down. Comparing mid with the target tells you nothing, so compare it with a reference point, find the break, then search a sorted piece.

Sorted in pieces​

A rotated array is two ascending runs; the minimum (the pivot) starts the second. A mountain rises to a peak and falls. In both, one comparison per probe says which side of the break mid is on.

Compare with a[hi]​

a[mid] > a[hi] means mid is still in the first run, so the pivot is to the right: lo = mid + 1. Otherwise hi = mid.

Compare with a[mid + 1]​

a[mid] < a[mid + 1] means mid is on the way up, so the peak is to the right: lo = mid + 1. Otherwise hi = mid.

Duplicates break the test​

When a[mid] == a[hi] the side is unknown. Drop one copy with hi -= 1; the worst case becomes O(n) (154, 81).

Find the pivot​

Find the Pivot (Minimum)​

Find the Pivot (Minimum)

Medium

Find the Pivot (Minimum)

FIG. FIND PIVOT● INTERACTIVE
visualization loads as you reach it
class Solution:
def findPivot(self, nums):
# Answers [0, len(nums)): the pivot is a real index, ties shrink hi by one
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[hi] < nums[mid]:
lo = mid + 1
elif nums[hi] > nums[mid]:
hi = mid
else:
hi -= 1
return lo
top ↑

153. Find Minimum in Rotated Sorted Array​

153. Find Minimum in Rotated Sorted Array

LeetCode
Medium

Find the Pivot, then Read It

FIG. 153 FIND MINIMUM IN ROTATED SORTED ARRAY● INTERACTIVE
visualization loads as you reach it
class Solution:
def findMin(self, nums: List[int]) -> int:
# Answers [0, len(nums)): the minimum is always a real index, so return nums[lo]
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = mid
return nums[lo]
top ↑

154. Find Minimum in Rotated Sorted Array II​

154. Find Minimum in Rotated Sorted Array II

LeetCode
Hard

Find the Pivot, then Read It

FIG. 154 FIND MINIMUM IN ROTATED SORTED ARRAY II● INTERACTIVE
visualization loads as you reach it
class Solution:
def findPivot(self, nums):
# Answers [0, len(nums)): the pivot is a real index, ties shrink hi by one
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[hi] < nums[mid]:
lo = mid + 1
elif nums[hi] > nums[mid]:
hi = mid
else:
hi -= 1
return lo
def findMin(self, nums: List[int]) -> int:
pivot = self.findPivot(nums)
return nums[pivot]
top ↑

Find the pivot, then bisect whichever sorted run can hold the target.

33. Search in Rotated Sorted Array​

33. Search in Rotated Sorted Array

LeetCode
Medium

Find the Pivot, then Split

FIG. 33 SEARCH IN ROTATED SORTED ARRAY● INTERACTIVE
visualization loads as you reach it
class Solution:
def bisect_left(self, arr, target, lo: int = 0, hi: int = None):
# hi is exclusive. The insert position can never be >= len(arr)
hi = (hi or len(arr)) - 1
if lo == hi + 1: # if array is empty
return -1
if arr[hi] < target:
return hi + 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
 
def findPivot(self, nums):
# Answers [0, len(nums)): the pivot is a real index, ties shrink hi by one
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[hi] < nums[mid]:
lo = mid + 1
elif nums[hi] > nums[mid]:
hi = mid
else:
hi -= 1
return lo
def search(self, nums: List[int], target: int) -> int:
pivot = self.findPivot(nums)
left = self.bisect_left(nums, target, lo=0, hi=pivot)
if left < len(nums) and nums[left] == target:
return left
right = self.bisect_left(nums, target, lo=pivot, hi=len(nums))
if right < len(nums) and nums[right] == target:
return right
return -1
top ↑

Climb to the peak​

162. Find Peak Element​

162. Find Peak Element

LeetCode
Medium

Climb Toward the Slope

FIG. 162 FIND PEAK ELEMENT● INTERACTIVE
visualization loads as you reach it
class Solution:
def findPeakElement(self, nums: list[int]) -> int:
# Answers [0, len(nums)): a real index, and mid < hi keeps nums[mid+1] in bounds
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if nums[mid] < nums[mid + 1]:
lo = mid + 1
else:
hi = mid
return lo
top ↑

852. Peak Index in a Mountain Array​

852. Peak Index in a Mountain Array

LeetCode
Medium

Climb Toward the Slope

FIG. 852 PEAK INDEX IN A MOUNTAIN ARRAY● INTERACTIVE
visualization loads as you reach it
class Solution:
def peakIndexInMountainArray(self, arr: List[int]) -> int:
# Answers [0, len(arr)): a real index, and mid < hi keeps arr[mid + 1] in bounds
lo, hi = 0, len(arr) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] < arr[mid + 1]:
lo = mid + 1
else:
hi = mid
return lo
top ↑

Peak, then search both slopes​

Find the peak, bisect the rising slope, then the falling slope with the comparison flipped.

1095. Find in Mountain Array​

1095. Find in Mountain Array

LeetCode
Hard

Peak, then Search Both Slopes

FIG. 1095 FIND IN MOUNTAIN ARRAY● INTERACTIVE
visualization loads as you reach it
class Solution:
def bisect_left(self, arr, target, lo: int = 0, hi: int = None):
# hi is exclusive. The insert position can never be >= len(arr)
hi = (hi or len(arr)) - 1
if lo == hi + 1: # if array is empty
return -1
if arr[hi] < target:
return hi + 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
 
def bisect_left_rev(self, arr, target, lo: int = 0, hi: int = None):
# hi is exclusive. The insert position can never be >= len(arr)
hi = (hi or len(arr)) - 1
if lo == hi + 1: # if array is empty
return -1
if arr[hi] > target:
return hi + 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] > target:
lo = mid + 1
else:
hi = mid
return lo
 
def peakIndexInMountainArray(self, arr: List[int]) -> int:
# Answers [0, len(arr)): a real index, and mid < hi keeps arr[mid + 1] in bounds
lo, hi = 0, len(arr) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] < arr[mid + 1]:
lo = mid + 1
else:
hi = mid
return lo
def findInMountainArray(self, target: int, mountain_arr: "MountainArray") -> int:
mountain_arr = CustomMountainArray(mountain_arr)
peakIndex = self.peakIndexInMountainArray(mountain_arr)
left = self.bisect_left(mountain_arr, target, lo=0, hi=peakIndex)
if mountain_arr[left] == target:
return left
right = self.bisect_left_rev(mountain_arr, target, lo=peakIndex, hi=len(mountain_arr) - 1)
if right < len(mountain_arr) and mountain_arr[right] == target:
return right
return -1
 
 
class CustomMountainArray:
def __init__(self, ma):
self.mountain_arr = ma
 
def __len__(self):
return self.mountain_arr.length()
 
def __getitem__(self, key):
return self.mountain_arr.get(key)
top ↑