Solution: Search in Rotated Sorted Array
Explore how to apply a modified binary search to find a target value within a rotated sorted array. This lesson guides you through understanding which half of the array is sorted, adjusting search boundaries accordingly, and implementing an iterative solution that optimizes search with a time complexity of O(log n) and space complexity of O(1).
Statement
You are given a sorted integer array, nums, and an integer, target. The array may have been rotated by an arbitrary number. Your task is to find and return the index of target in this array. If target does not exist, return -1.
An original sorted array before rotation is given below:
After rotating this array 6 times, it changes to:
Constraints:
- All values in
numsare unique. - The values in
numsare sorted in ascending order. - The array may have been rotated by some arbitrary number.
-
nums.length -
nums[i]