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>

  1. payam rastogi on 2015-2-22

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

  2. ryanlr on 2015-2-23

    The last element is handled separately in my code.

  3. payam rastogi on 2015-2-23

    its about the second last element

  4. dn3point on 2015-3-24

    Why not just find the max number in the array?

  5. CodeMonkey on 2015-4-11

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

  6. GoatGuy on 2015-4-16

    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

  7. Taoufik Bdiri on 2015-4-30

    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. Taoufik Bdiri on 2015-4-30

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

  9. beeflamian on 2015-5-6

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

  10. Jason Zhu on 2015-6-17

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

    }

  11. Jason Zhu on 2015-6-17

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

  12. Jason Zhu on 2015-6-17

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

  13. Shannon McNeill on 2015-7-17

    wrap the code in <pre%gt; tags

  14. Rashmi Chaudhary on 2015-9-8

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

  15. Walden on 2016-1-2

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

  16. rmarathe on 2016-1-12

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

  17. rupalph on 2016-9-3

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

Leave a comment

*