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;
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • rupalph

    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);
    }

  • rmarathe

    //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;
    }

  • Walden

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

  • Rashmi Chaudhary

    Check this video on finding peak element that solves the problem in O(log n) time:
    https://www.youtube.com/watch?v=a7D77DdhlFc

  • Shannon McNeill

    wrap the code in <pre%gt; tags

  • Jason Zhu

    what about my latest answer (in the above) to this question for O(logN) complexity?

  • Jason Zhu

    Could anyone tell me how to fomat the code, please?

  • Jason Zhu

    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];

    }

  • beeflamian

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

  • You’re right. The proposed solution is looking for the max value instead of local peaks ! Look at my comment above !

  • 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;
    }
    }

  • GoatGuy

    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

  • CodeMonkey

    We can reduce the time complexity of this question, dude. Do binary search instead of linear.

  • dn3point

    Why not just find the max number in the array?

  • its about the second last element

  • ryanlr

    The last element is handled separately in my code.

  • in “for(int i=1; i<=num.length-2; i++)" condition should be "i<=num.length-1"