Search in Rotated Sorted Array
Given an integer array nums sorted in ascending order and rotated at an unknown pivot, and an integer target, return the index of target if it exists in nums.
Otherwise, return -1.
You must write an algorithm with O(log n) runtime complexity.
All values in nums are distinct.
Example 1
Input
n = 7 nums = [4, 5, 6, 7, 0, 1, 2] target = 0
Output
4
Explanation
The target 0 is present at index 4.
Example 2
Input
n = 7 nums = [4, 5, 6, 7, 0, 1, 2] target = 3
Output
-1
Explanation
The target 3 does not exist in the array.
Constraints
Hints:
Hint 1
At every step, at least one half of the array is normally sorted.
Hint 2
Check whether the left half is sorted:
nums[left] <= nums[mid]
Then determine whether the target lies inside that sorted range.
Otherwise, search the other half.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 22, 2026
Last updated
Aug 22, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output