@@ -0,0 +1,1631 @@
+public class FindPeakElement %7B%0A    /**%0A    * A peak element is an element that is greater than its neighbors.%0A    * Given an input array where num%5Bi%5D %E2%89%A0 num%5Bi+1%5D, find a peak element and return its index.%0A    * The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.%0A    * You may imagine that num%5B-1%5D = num%5Bn%5D = -%E2%88%9E.%0A    * For example, in array %5B1, 2, 3, 1%5D, 3 is a peak element and your function should return the index number 2.%0A    */%0A    public int findPeakElement(int%5B%5D nums) %7B%0A        return findPeakOverRange (nums, 0, nums.length - 1, nums.length);%0A    %7D%0A    %0A    private int findPeakOverRange (int%5B%5D nums, int low, int high, int capacity) %7B%0A        /**%0A        * Recursively find peak in the range nums%5Blow%5D to nums%5Bhigh%5D using binary search%0A        */%0A        // to avoid overflow%0A        int mid = low + (high - low) / 2;%0A        // condition %7Clow%7Chigh%7Clow%7C%0A        if ((mid == 0 %7C%7C nums%5Bmid - 1%5D %3C= nums%5Bmid%5D) && (mid == capacity - 1 %7C%7C nums%5Bmid + 1%5D %3C= nums%5Bmid%5D)) return mid;%0A        // search left half for a peak%0A        else if (mid %3E 0 && nums%5Bmid - 1%5D %3E nums%5Bmid%5D) return findPeakOverRange (nums, low, mid - 1, capacity);%0A        // search right half for a peak%0A        else return findPeakOverRange (nums, mid + 1, high, capacity);%0A    %7D%0A    %0A    public static void main (String%5B%5D args) %7B%0A        FindPeakElement test = new FindPeakElement ();%0A        int%5B%5D nums1 = %7B1, 2, 3, 1%7D;%0A        int%5B%5D nums2 = %7B23, 21, 20, 21, 19, 17, 16%7D;%0A        System.out.println (test.findPeakElement (nums1) == 2);%0A        System.out.println (test.findPeakElement (nums2) == 3);%0A    %7D%0A%7D

Find peak element in array