Find a Value in a Shifted Sorted Array
Heaps, Sorting & SearchAuto-graded · 27 test casesLast updated
Tests whether a candidate can run a single-pass binary search on a sorted array that has been cyclically shifted by an unknown offset.
Problem
You have a list of integers that was originally sorted in strictly increasing order with no repeated values. At some point, the list was "shifted" — a suffix of the array was moved to the front. For example, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2] after a shift.
Given this shifted list and a target value, return the 0-based index of the target if it appears in the list, or -1 if it does not.
Your solution must run in O(log n) time using a single binary-search pass — you may not first locate the shift point and then do a separate search. The input list must not be modified.
How to Approach It
At any midpoint you pick, one of the two halves is always an unbroken ascending run. Use the endpoints of that sorted half to decide whether the target could live there, and shrink the window accordingly.
Constraints
1 <= len(arr) <= 10^5-10^9 <= arr[i] <= 10^9- All values in
arrare distinct. - The array may or may not have been shifted (an unshifted array is a valid input).
-10^9 <= target <= 10^9
Examples
Example 1
Input: find_in_shifted_array([4, 5, 6, 7, 0, 1, 2], 0)
Output: 4
Example 2
Input: find_in_shifted_array([4, 5, 6, 7, 0, 1, 2], 3)
Output: -1
Example 3
Input: find_in_shifted_array([1], 0)
Output: -1
Hints
Hint 1
Split the window at mid. One of [left, mid] or [mid, right] must be a clean ascending run — figure out which one by comparing arr[left] with arr[mid].
Hint 2
Once you know which half is sorted, its two endpoints tell you exactly whether the target can be inside it. If it can, search there; otherwise search the other half.
Hint 3
Be careful with the comparison operator when checking whether the left half is sorted. When the window shrinks to two elements, left == mid, so you need <= rather than < to avoid misclassifying that case.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core observation
A cyclically shifted sorted array has at most one "drop" — a single point where a larger value is immediately followed by a smaller one. If you cut the array at any midpoint mid, that drop can fall in the left half or the right half, but not in both. Whichever half does not contain the drop is an ordinary ascending run, and its two endpoints tightly bound every value inside it.
Algorithm
Maintain a window [left, right] that always contains the target index if the target exists.
- Compute
mid = (left + right) // 2. - If
arr[mid] == target, returnmid. - Check which half is the sorted one:
arr[left] <= arr[mid]→ the left half[left, mid]is sorted.- Otherwise → the right half
[mid, right]is sorted.
- Use the sorted half's endpoints to route the search:
- Left sorted: if
arr[left] <= target < arr[mid], the target can only be in[left, mid-1]; setright = mid - 1. Otherwise setleft = mid + 1. - Right sorted: if
arr[mid] < target <= arr[right], setleft = mid + 1. Otherwise setright = mid - 1.
- Left sorted: if
- If the window empties (
left > right), return-1.
Why <= and not <
When the window holds exactly two elements, left == mid, so arr[left] == arr[mid]. The <= comparison correctly classifies the one-element "left half" as sorted. A strict < would fall into the wrong branch — for example, arr = [3, 1], target = 1 would be mishandled.
Why it is correct
Invariant: if target is in arr, its index is in [left, right].
The invariant holds at the start (full array). At each step we only discard a half that the range check has proven cannot contain the target. When the window empties the invariant tells us the target is absent.
Complexity
- Time: O(log n) — the window halves on every iteration, and each iteration does O(1) work.
- Space: O(1) — only a fixed number of index variables are used; the input is not modified.
Edge cases
- Single element:
mid = 0, no match, window empties, returns-1. - Two elements: the
<=handles theleft == midsituation correctly. - No shift:
arr[left] <= arr[mid]is always true, so the algorithm degrades to standard binary search. - Target at the ends: the inclusive bounds
arr[left] <= targetandtarget <= arr[right]keep both endpoints reachable. - Target adjacent to the shift point: the range check on the sorted half correctly routes the search across the drop.
Reference solution
from typing import List
def find_in_shifted_array(arr: List[int], target: int) -> int:
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
# Determine which half is a clean ascending run.
if arr[left] <= arr[mid]:
# Left half [left, mid] is sorted.
if arr[left] <= target < arr[mid]:
right = mid - 1 # target must be in the sorted left half
else:
left = mid + 1 # target is somewhere in the right half
else:
# Right half [mid, right] is sorted.
if arr[mid] < target <= arr[right]:
left = mid + 1 # target must be in the sorted right half
else:
right = mid - 1 # target is somewhere in the left half
return -1