Find peak in mountain array public class Main { public static int peak(int arr[], int s, int e){ int m=(s+e)/2; if(arr[m]>arr[m+1] &&arr[m]>arr[m-1]){ return m; } if(arr[m]<arr[m-1]){ return peak(arr,s,m-1); } else if(arr[m]<arr[m+1]) return peak(arr,m+1,e); return m; } public static void main(String[] args) { System.out.println("Hello World "); int arr[]={0,1,2,5,4,3,1}; System.out.println(arr[peak(arr,0,5)]); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter