33. Search in Rotated Sorted Array

Practice Search in Rotated Sorted Array for free on NudgeCode: Medium, Binary Search pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

nums was sorted ascending with distinct values, then possibly rotated: some prefix was cut off and moved to the end (e.g. [0,1,2,4,5,6,7] -> [4,5,6,7,0,1,2]). Return the index of target, or -1 if it isn't there, in O(log n).

Example

nums = [4,5,6,7,0,1,2], target = 0  ->  4
nums = [4,5,6,7,0,1,2], target = 3  ->  -1

Constraints