All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0162_find_peak_element.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0101_0200.s0162_find_peak_element;

// #Medium #Top_Interview_Questions #Array #Binary_Search

public class Solution {
    public int findPeakElement(int[] nums) {
        int start = 0;
        int end = nums.length - 1;

        while (start < end) {
            // This is done because start and end might be big numbers, so it might exceed the
            // integer limit.
            int mid = start + ((end - start) / 2);

            if (nums[mid + 1] > nums[mid]) {
                start = mid + 1;
            } else {
                end = mid;
            }
        }

        return start;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy