Search in Rotated Sorted Array
Problem statement
You are given an integer array nums containing distinct values. It was sorted in increasing order and then rotated at an unknown pivot.
Return the index of target if it appears in nums; otherwise return -1. Your algorithm must run in O(log n) time.
Function
searchRotated(nums: int[], target: int) → intExamples
Example 1
nums = [4,5,6,7,0,1,2]target = 0return = 4The target appears at index 4.
Example 2
nums = [4,5,6,7,0,1,2]target = 3return = -1The target is absent.
Constraints
1 <= nums.length <= 100000.- Every value in
numsis distinct and in[-10^9, 10^9]. numsis a rotation of a strictly increasing array.targetis in[-10^9, 10^9].
Source note: Author-written prompt excerpt retained for source fidelity.