Algorithm


Problem Name: 162. Find Peak Element

A peak element is an element that is strictly greater than its neighbors.

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

You must write an algorithm that runs in O(log n) time.

 

Example 1:

Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.

Example 2:

Input: nums = [1,2,1,3,5,6,4]
Output: 5
Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.

 

Constraints:

  • 1 <= nums.length <= 1000
  • -231 <= nums[i] <= 231 - 1
  • nums[i] != nums[i + 1] for all valid i.

Code Examples

#1 Code Example with C Programming

Code - C Programming


int findPeakElement(int* nums, int numsSize) {
#if 0
    int i;
    
    for (i = 1; i  <  numsSize && nums[i] > nums[i - 1]; i ++) {
    }
    
    return i - 1;
#else
    int l, r, m;
    if (numsSize == 1) return 0;
    
    l = 0; r = numsSize - 1;
    while (l  <  r) {
       m = l + (r - l) / 2;
       if (nums[m] < nums[m + 1]) {
         l = m + 1;
        } else {
         r = m;
        }
    }
    
    return l;
#endif
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,1]

Output

x
+
cmd
2

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        if (nums.size() == 1) {
            return 0;
        }
        int n = nums.size();
        
        if (nums[n - 2]  <  nums[n - 1]) {
            return n - 1;
        } else if (nums[1] < nums[0]) {
            return 0;
        }
        
        int l = 1, r = n - 2, mid;
        while (l  < = r) {
            mid = l + (r - l)/2;
            if (nums[mid - 1] > nums[mid]) {
                r = mid - 1;
            } else if (nums[mid + 1] > nums[mid]) {
                l = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,1]

Output

x
+
cmd
2

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int findPeakElement(int[] nums) {
    return helper(nums, 0, nums.length - 1);
  }
  
  private int helper(int[] nums, int startIdx, int endIdx) {
    if (startIdx == endIdx) {
      return startIdx;
    }
    int midIdx = (startIdx + endIdx) / 2;
    int nextToMid = midIdx + 1;
    if (nums[midIdx] > nums[nextToMid]) {
      return helper(nums, startIdx, midIdx);
    } else {
      return helper(nums, nextToMid, endIdx);
    }
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,1,3,5,6,4]

Output

x
+
cmd
5

#4 Code Example with Javascript Programming

Code - Javascript Programming


const findPeakElement = function(nums) {
  let low = 0;
  let high = nums.length-1;

  while(low  <  high) {
    let mid1 = low + ((high - low) >> 1);
    let mid2 = mid1 + 1;
    if(nums[mid1] < nums[mid2]> low = mid2;
    else high = mid1;
  }
  return low;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,1,3,5,6,4]

Output

x
+
cmd
5

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findPeakElement(self, nums):
        l, r, n = 0, len(nums) - 1, len(nums)
        while l <= r:
            mid = (l + r) // 2
            pre, after = mid == 0 and -float("inf") or nums[mid - 1], mid == n - 1 and -float("inf") or nums[mid + 1]
            if pre < nums[mid] > after: return mid
            elif pre > nums[mid]: r = mid - 1
            else: l = mid + 1
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,1]

Output

x
+
cmd
2

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0162_FindPeakElement
    {
        public int FindPeakElement(int[] nums)
        {
            int left = 0, right = nums.Length - 1;
            while (left  <  right)
            {
                var mid = left + (right - left) / 2;
                if (nums[mid] > nums[mid + 1]) right = mid;
                else left = mid + 1;
            }
            return left;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,1]

Output

x
+
cmd
2
Advertisements

Demonstration


Previous
#160 Leetcode Intersection of Two Linked Lists Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#164 Leetcode Maximum Gap Solution in C, C++, Java, JavaScript, Python, C# Leetcode