-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_In_Mountain_Array
54 lines (54 loc) · 1.24 KB
/
Find_In_Mountain_Array
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
int findInMountainArray(int target, MountainArray &mountainArr) {
int n=mountainArr.length();
int low=0;
int high=n-1;
while(low<high)
{
int mid=low+(high-low)/2;
if(mountainArr.get(mid)<mountainArr.get(mid+1))
{
low=mid+1;
}
else
{
high=mid;
}
}
int low1=0;
int high1=high;
int low2=high;
int high2=n-1;
while(low1<=high1)
{
int mid=low1+(high1-low1)/2;
if(mountainArr.get(mid)==target)
{
return mid;
}
else if(mountainArr.get(mid)>target)
{
high1=mid-1;
}
else
{
low1=mid+1;
}
}
while(low2<=high2)
{
int mid=low2+(high2-low2)/2;
if(mountainArr.get(mid)==target)
{
return mid;
}
else if(mountainArr.get(mid)>target)
{
low2=mid+1;
}
else
{
high2=mid-1;
}
}
return -1;
}