package com.interview.binarysearch;

public class PeakElement {

    public int peakElement(int arr[]){
        int low = 0;
        int high = arr.length-1;
        while(low < high){
            int mid = (low+high)/2;
            if(mid == 0 || mid == arr.length-1){
                return arr[mid];
            }
            if(arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]){
                return arr[mid];
            }
            if(arr[mid] < arr[mid-1]){
                high = mid-1;
                continue;
            }
            if(arr[mid] < arr[mid+1]){
                low = mid+1;
                continue;
            }
        }
        return arr[low];
    }
    
    public static void main(String args[]){
        int arr[] = {10,5,15,2,23,90,67};
        PeakElement pe = new PeakElement();
        System.out.println(pe.peakElement(arr));
        int arr1[] = {10,20,30,40,50};
        System.out.println(pe.peakElement(arr1));
        int arr2[] = {100,90,80,70,60};
        System.out.println(pe.peakElement(arr2));
                
    }
}

@@ -30,16 +30,287 @@
 earch;%0A%0A
+/**%0A * @author Tushar Roy%0A * Date 01/17/2107%0A * A peak element is an element that is greater than its neighbors. Find index of peak element in the array.%0A *%0A * Space complexity is O(1)%0A * Time complexity is O(n)%0A *%0A * https://leetcode.com/problems/find-peak-element/%0A */%0A
 public c
@@ -344,17 +344,21 @@
 lic int 
-p
+findP
 eakEleme
@@ -367,15 +367,17 @@
 (int
- arr%5B%5D)
+%5B%5D nums) 
 %7B%0A  
@@ -418,22 +418,49 @@
 h = 
-arr
+nums
 .length
--
+ - 
 1;%0A
+        int middle = 0;%0A
     
@@ -472,20 +472,23 @@
 hile
+ 
 (low %3C
+=
  high)
+ 
 %7B%0A  
@@ -501,23 +501,24 @@
     
-int 
 mid
+dle
  = (low
-+
+ + 
 high
@@ -539,239 +539,332 @@
    i
-f(mid == 0 %7C%7C mid == arr.length-1)%7B%0A                return arr%5Bmid%5D;%0A            %7D%0A            if(arr%5Bmid%5D %3E arr%5Bmid-1%5D && arr%5Bmid%5D %3E arr%5Bmid+1%5D)%7B%0A                return arr%5Bmid%5D;%0A            %7D%0A            if(arr%5Bmid%5D %3C arr%5Bmid-1%5D)
+nt before = Integer.MIN_VALUE;%0A            if (middle %3E 0) %7B%0A                before = nums%5Bmiddle - 1%5D;%0A            %7D%0A            int after = Integer.MIN_VALUE;%0A            if (middle %3C nums.length - 1) %7B%0A                after = nums%5Bmiddle + 1%5D;%0A            %7D%0A            if (nums%5Bmiddle%5D %3E before && nums%5Bmiddle%5D %3E after) 
 %7B%0A  
@@ -881,20 +881,21 @@
     
-high =
+return
  mid
--1
+dle
 ;%0A  
@@ -908,36 +908,43 @@
     
-    continue;%0A  
+%7D else if (before %3E after) %7B%0A
       
-    %7D%0A
     
@@ -953,65 +953,47 @@
     
-  if(arr%5Bmid%5D %3C arr%5Bmid+1%5D)%7B%0A                low = mid+1;
+high = middle - 1;%0A            %7D else %7B
 %0A   
@@ -1009,16 +1009,24 @@
     
-continue
+low = middle + 1
 ;%0A  
@@ -1066,24 +1066,22 @@
 urn 
-arr%5Blow%5D
+middle
 ;%0A    %7D%0A
     
@@ -1076,20 +1076,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -1229,33 +1229,37 @@
 .out.println(pe.
-p
+findP
 eakElement(arr))
@@ -1321,33 +1321,37 @@
 .out.println(pe.
-p
+findP
 eakElement(arr1)
@@ -1423,17 +1423,21 @@
 ntln(pe.
-p
+findP
 eakEleme

Update peak element