Loading...
Loading...
Loading...
A sorted list of distinct integers was rotated, for example 30 40 50 10 20. Read a target and print its position (counting from 0), or -1 if it is missing. Use O(log n) time.
Example 1
Input
5 30 40 50 10 20 10
Output
3
Example 2
Input
5 30 40 50 10 20 35
Output
-1
Try the problem first. Open one hint at a time.
Around the middle, at least one half is sorted. Test a[lo] ≤ a[mid] to see whether the left half is.
If the target lies inside the sorted half, keep that half. Otherwise keep the other one.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor