-
Notifications
You must be signed in to change notification settings - Fork 2
/
cyclic_binary_search.cpp
68 lines (58 loc) · 1.52 KB
/
cyclic_binary_search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Rahul had a sorted array of numbers from which he had to find a given number quickly. His friend by mistake rotated the array. Now Rahul doesn't have time to sort the elements again. Help him to quickly find the given number from the rotated array.
Hint - Think Binary Search!
Input Format
The first line contains N - the size of the array. the next N lines contain the numbers of the array.The next line contains the item to be searched.
Constraints
Output Format
Output the index of number in the array to be searched. Output -1 if the number is not found.
Sample Input
5
4
5
1
2
3
2
Sample Output
3
Explanation
The given rotated array is [4, 5, 1, 2, 3]. The element to be searched is 2 whose index is 3.
*/
#include <iostream>
#include <algorithm>
using namespace std;
int find(int *arr, int n, int key) {
int start = 0, end = n - 1;
while(start <= end) {
int mid = start + (end - start) / 2;
if(arr[mid] == key)
return mid;
else if(arr[mid] >= arr[start]) {
// part-1 of the array
// here we have 2 cases --> left / right of the mid.
if(key >= arr[start] and key < arr[mid])
end = mid - 1;
else
start = mid + 1;
}
else {
// part-2 of the array
if(key > arr[mid] and key <= arr[end])
start = mid + 1;
else
end = mid - 1;
}
}
return -1; // element not found
}
int main() {
int n, key;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
cin >> key;
cout << find(arr, n, key);
return 0;
}