# [Leetcode] 1095. Find in Mountain Array

Hi,
I was working on

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

And I ran into a problem with the following case
[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,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82]
81

Here is my code
/**

• // This is the MountainArray’s API interface.
• // You should not implement it, or speculate about its implementation
• class MountainArray {
• public:

int get(int index);

``````
* ```
int length();
``````
• };
*/

class Solution {
public:
int findInMountainArray(int target, MountainArray &A)
{
// first identify peak
// search left and then search right

``````    int n=A.length();

// peak
int peak=-1;
int start=0;
int end=n-1;
while(start+1<end)
{
int mid=(start+(end-start)/2);

if(A.get(mid-1)<A.get(mid)&& A.get(mid)>A.get(mid-1))
{
peak=mid;
break;
}
if(A.get(mid)<A.get(mid-1))
{
end=mid;
}
else
{
start=mid;
}
}
cout<<"Start: "<<start<<endl;
if(A.get(start)>A.get(start+1) && A.get(start)>A.get(start-1))
{
peak=start;
}

if(end!=n-1 && A.get(end)>A.get(end+1) && A.get(start)>A.get(end-1))
{
peak=start;
}
cout<<peak<<endl;
if(peak==-1)
return -1;

//left
start=0;
end=peak;
while(start+1<end)
{
int mid=(start+(end-start)/2);

if(A.get(mid)==target)
{
return mid;
}
if(target>A.get(mid))
{
start=mid;
}
else
{
end=mid;
}
}

cout<<"End after left: "<<end<<endl;
cout<<"Start after left: "<<start<<endl;
// left post processing
if(A.get(start)==target)
return start;
if(A.get(end)==target)
return end;

//right
start=peak+1;
end=n-1;

while(start+1<end)
{
int mid=(start+(end-start)/2);

if(A.get(mid)==target)
{
return mid;
}
if(target<A.get(mid))
{
start=mid;
}
else
{
end=mid;
}
}

cout<<"End after right: "<<end<<endl;
cout<<"Start after right: "<<start<<endl;
// right post processing
if(A.get(start)==target)
return start;
if(A.get(end)==target)
return end;

return -1;
}
``````

};

Where am I going worng?

One thing I noticed is that if you decided to use
`while(start+1<end)`

as the condition for binary search, you will need to do some post-processing on start and end when while loop terminated