Find Peak Element in Python


Suppose we have to find the peak element in an array. The peak element is an element that is greater than its neighbors. Suppose we have an input array nums, where nums[i] ≠ nums[i+1], search for a peak element and return its index. The array can hold multiple peak elements, in that case return the index to any one of the peak elements. We can imagine that nums[-1] = nums[n] = -∞. So if the array is like [1,2,1,3,5,6,4], the peak elements should be 1 or 5.

To solve this, we will follow these steps −

  • low := 0 and high := last index of array, n := size of array, ans := infinity
  • while low <= high
    • mid := low + (high - low)/2
    • if mid – 1 >= 0 and nums[mid – 1] <= nums[mid], then low := mid, otherwise high := mid - 1
  • return low

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution(object):
   def findPeakElement(self, nums):
      low = 0
      high = len(nums)-1
      while low<high:
         mid = low + (high - low+1)//2
         if (mid-1>=0 and nums[mid-1]<=nums[mid]):
            low = mid
         else:
            high = mid-1
      return nums[low+1]
ob1 = Solution()
print(ob1.findPeakElement([15,35,85,96,5,6,8,12]))

Input

[15,35,85,96,5,6,8,12]

Output

5

Updated on: 04-May-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements