# A very simple accepted non-recursive binary search java solution

• The idea is to perform a simple binary serach with a little twist. If mid is the peak then return else keep searching in the direction which is higher of two - (mid -1) & (mid + 1).

I referred the MIT course notes to get to this solution - http://courses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf

``````public int findPeakElement(int[] num) {
if(num == null || num.length < 1) {
return -1;
}
int start = 0;
int end = num.length - 1;
int mid;
long left, right;

while(start <= end) {
mid = (start + end)/2;
left = mid == 0 ? (long)Integer.MIN_VALUE - 1 : num[mid - 1];
right = mid == num.length - 1 ? (long)Integer.MIN_VALUE - 1: num[mid + 1];

if(left < num[mid] && right < num[mid]) {
return mid;
}
if(left < right) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
``````

• If you have the same lecture by Eric Demiane that will be great

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.