Imagine you have a sorted library bookshelf that someone accidentally rotated! ๐
You're given an integer array nums that was originally sorted in ascending order with distinct values. However, before reaching you, this array was left rotated at some unknown pivot point k, where 1 โค k < nums.length.
What does rotation mean?
If we rotate [0,1,2,4,5,6,7] left by 3 positions, we get [4,5,6,7,0,1,2]. The elements [4,5,6,7] moved to the front, and [0,1,2] wrapped around to the back.
Your mission: Find the index of a target value in this rotated array, or return -1 if it doesn't exist.
The catch: You must solve this in O(log n) time complexity - no linear scanning allowed!
Examples:
โข nums = [4,5,6,7,0,1,2], target = 0 โ return 4
โข nums = [4,5,6,7,0,1,2], target = 3 โ return -1
Input & Output
Constraints
- 1 โค nums.length โค 5000
- -104 โค nums[i] โค 104
- All values of nums are unique
- nums is an ascending array that is possibly rotated
- -104 โค target โค 104