Watch 10 video solutions for Search in Rotated Sorted Array, a medium level problem involving Array, Binary Search. This walkthrough by NeetCode has 413,348 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1
Example 3:
Input: nums = [1], target = 0 Output: -1
Constraints:
1 <= nums.length <= 5000-104 <= nums[i] <= 104nums are unique.nums is an ascending array that is possibly rotated.-104 <= target <= 104Problem Overview: You are given a sorted array that has been rotated at some unknown pivot. The order is still increasing within segments, but the rotation breaks the global order. The task is to locate a target value and return its index in O(log n) time, or -1 if the target does not exist.
Approach 1: Binary Search with Rotation Logic (O(log n) time, O(1) space)
This approach modifies classic Binary Search to work with rotated ordering. During each iteration, compute the midpoint and determine which half of the array is normally sorted. In a rotated array, at least one half between left and mid or between mid and right must remain sorted. Once you identify the sorted side, check whether the target lies within that range. If it does, continue searching there; otherwise move to the other half. The key insight is that the sorted segment gives you a reliable comparison boundary even after rotation. This keeps the search space halving each step, preserving O(log n) time with constant memory.
The algorithm works well because it combines rotation detection and search in a single pass. Each iteration performs simple comparisons and pointer adjustments. No additional preprocessing is required. This is the approach most developers implement in interviews because it demonstrates a strong understanding of conditional search logic and edge handling.
Approach 2: Find Rotated Index and Execute Two Binary Searches (O(log n) time, O(1) space)
This method separates the problem into two clear stages. First, locate the rotation pivot — the index where the smallest element appears. The pivot can be found using a modified binary search that compares mid with the right boundary to determine which half contains the minimum. Once the pivot is identified, the array is effectively split into two sorted subarrays.
Next, run a standard binary search on the appropriate half depending on where the target could exist. If the target lies between the first element and the element just before the pivot, search the left segment; otherwise search the right segment. Because both segments are individually sorted, normal binary search works without additional checks. This technique is conceptually clean and often easier to reason about for beginners studying rotated arrays in Array and Binary Search problems.
Recommended for interviews: The single-pass binary search with rotation logic is typically expected. It achieves O(log n) time without explicitly finding the pivot and shows that you can reason about partially sorted structures. Demonstrating the pivot approach first can help explain the rotation concept, but implementing the single-pass method proves stronger mastery of binary search variants.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Binary Search with Rotation Logic | O(log n) | O(1) | Best general solution. Handles rotation and search in one pass. |
| Find Pivot then Binary Search | O(log n) | O(1) | Useful when learning rotated arrays or when pivot detection logic is needed separately. |