P14386 [GeeksforGeeks] Search in a rotated array4618
普及-
题目描述

Given a sorted and rotated array A of N distinct elements which is rotated at some point, and given an element key. The task is to find the index of the given element key in the array A. The whole array A is given as the range to search.
Example 1:
Input:
N = 9
A[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}
key = 10
l = 0 , h = 8
Output:
5
Explanation: 10 is found at index 5.
Example 2:
Input:
N = 4
A[] = {3, 5, 1, 2}
key = 6
l = 0 , h = 3
Output:
-1
Explanation: There is no element that has value 6.
Your Task:
Complete the function search() which takes an array arr[] and start, end index of the array and the K as input parameters, and returns the answer.
Can you solve it in expected time complexity?
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{7}
0 ≤ A[i] ≤ 10^{8}
1 ≤ key ≤ 10^{8}

输入格式

见题面

输出格式

见题面

样例输入
N = 9 A[] = {5, 6, 7, 8, 9, 10, 1, 2, 3} key = 10 l = 0 , h = 8
样例输出
5