-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotated_sorted_array.cpp
65 lines (54 loc) · 957 Bytes
/
Rotated_sorted_array.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
#include <bits/stdc++.h>using namespace std;
int search(vector<int> A, int B)
{
int low = 0,
high = A.size() - 1;
int mid = (low + high) / 2;
while (high >= low)
{
mid = (low + high) / 2;
if (A[mid] == B)
{
return mid;
}
else if (A[mid] > A[low])
{
if (B >= A[low] && B < A[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
else
{
if (A[mid] < B && B <= A[high])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
}
return -1;
}
int main()
{
vector<int> A = {
180,
181,
182,
183,
3,
4,
5,
6,
7,
8}
;
cout << search(A, 182);
}