-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.2.cpp
51 lines (48 loc) · 1.07 KB
/
2.2.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
#include <iostream>
int binarySearch(const int *arr, int left, int right){
if (right + 1 == 2) {
if (arr[0] < arr[1])
return 1;
else
return 0;
}
if (arr[0] > arr[1])
return 0;
if (arr[right - 1] < arr[right])
return right;
int mid = 0;
while (left != right){
mid = (left + right) / 2;
if (arr[mid] > arr[left])
left = mid;
else
right = mid;
}
return mid;
}
int preparation_to_binarySearch(const int *arr, int len){
int left = 0, right = 0;
for (int i = 1; i < len; i = i * 2)
{
left = i / 2;
if (arr[i] < arr[i / 2])
{
right = i;
break;
} else
right = len - 1;
}
return binarySearch(arr, left, right);
}
int main() {
int len, temp;
std::cin >> len;
int *a = new int[len];
for(int i = 0; i < len; i++) {
std::cin >> temp;
a[i] = temp;
}
std::cout << preparation_to_binarySearch(a, len) << std::endl;
delete[] a;
return 0;
}