There is an integer array nums sorted in ascending order (with distinct values), but it has been rotated at an unknown pivot.
Given the array nums and an integer target,
return the index of target if it exists in the array,
otherwise return -1.
You must write an algorithm with O(log n) time complexity.
nums)targetInput:
[4,5,6,7,0,1,2]
0
Output: 4
Input:
[4,5,6,7,0,1,2]
3
Output: -1
Input:
[1]
0
Output: -1
Your program must print only a single integer (the index of the target), so it can be compared with the expected output.
No submissions yet.
Discuss binary search logic, how to detect the sorted half, and how rotation affects comparisons.