LeetCode – Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

Thoughts

This is a very simple problem. We can scan the array and find any element that is greater can its previous and next. The first and last element are handled separately.

Java Solution

public class Solution {
    public int findPeakElement(int[] num) {
        int max = num[0];
        int index = 0;
        for(int i=1; i<=num.length-2; i++){
            int prev = num[i-1];
            int curr = num[i];
            int next = num[i+1];
 
            if(curr > prev && curr > next && curr > max){
                index = i;
                max = curr;
            }
        }
 
        if(num[num.length-1] > max){
            return num.length-1;
        }
 
        return index;
    }
}

18 thoughts on “LeetCode – Find Peak Element”

  1. Hey,
    I think the question is a bit unclear.
    What does it mean “A peak element is an element that is greater than its neighbors.”?
    Its neighbors, meaning just the two on both sides? If so, why have max in the solution?
    Also, if the array contains just a single repeated number, like [1,1,1,1,1,1], the solution would return 0, where actually there’s no peak by definition.

  2. O(logn) solution, call findPeak(arr,1,arr.length-1,arr[0])

    public static int findPeak(int[] arr,int i,int j,int max)
    {
    int mid = (i+j)/2;
    if(i>j)
    return max;
    if(i==j)
    return max>arr[i]?max:arr[i];

    if(arr[mid]>max)
    return findPeak(arr,mid+1,j,arr[mid]);
    else
    return findPeak(arr,i,mid-1,max);
    }

  3. //here is an accepted solution in java
    public int findPeakElement(int[] nums) {

    if(nums == null || nums.length ==0){
    return -1;
    }
    int i=0,j=1;
    while(j< nums.length && nums[i]<=nums[j]){
    i++;
    j++;
    }
    return i;
    }

  4. It is faster to find the first peak element rather than finding the maximum. Especially using the method shown in the video above.

  5. Hi, I’ve solved this problem in O(logN) time complexity, if anything wrong, plz tell me @ [email protected] :]

    public static int solution(int[] arr) {

    assert (arr != null && arr.length > 0);

    if(arr.length == 1) return arr[0];

    if(arr[0] > arr[1]) return arr[0];

    if(arr[arr.length-1] > arr[arr.length-2]) return arr[arr.length-1];

    int left = 0;

    int right = arr.length-1;

    while(left arr[mid+1]) right=mid-1;

    else left=mid+1;

    }

    return arr[left];

    }

  6. You can do binary search for this problem and I believe that is the point of this problem

  7. I believe this solution is more than what as it as been asked. You’re looking for a global peak but the requirement is to look for a local peak. You should return as soon as you find a local peak. My solution accepted by leet code :


    public class Solution {
    public int findPeakElement(int[] nums) {
    if (nums == null || nums.length nums[1])
    return 0;
    if (nums[nums.length - 1] > nums[nums.length - 2])
    return nums.length - 1;
    for (int i = 1; i nums[i - 1] && nums[i] > nums[i + 1])
    return i;
    }
    return -1;
    }
    }

  8. No you can’t, it wasn’t specified as being ordered. If it was ordered, then it would be trivial, just choose the last element. The only way to complete this is the O(n) linear search.

    This finds the MAX value (which may not correspond to a PEAK value):

    int top = num[0];
    int ndx = 0;
    for( int i = 1; i top ) { top = num; ndx = i; }
    }
    return ndx;

    The algorithm in the OP’s text finds the last PEAK element, not the first. It could be short circuited perhaps to find the first PEAK element, by simply returning of the 3-way condition is met. I mean they said, “there could be multiple peaks, returning any one of them is OK”.

    Kind of a dumb problem, actually. Ambiguous.

    GoatGuy

Leave a Comment