Find a peak element
Given an array of integers. Find a peak element in it. An array element is peak if it is NOT smaller than its neighbors. For corner elements, we need to consider only one neighbor. For example, for input array {5, 10, 20, 15}, 20 is the only peak element. For input array {10, 20, 15, 2, 23, 90, 67}, there are two peak elements: 20 and 90. Note that we need to return any one peak element.
Following corner cases give better idea about the problem.
1) If input array is sorted in strictly increasing order, the last element is always a peak element. For example, 50 is peak element in {10, 20, 30, 40, 50}.
2) If input array is sorted in strictly decreasing order, the first element is always a peak element. 100 is the peak element in {100, 80, 60, 50, 20}.
3) If all elements of input array are same, every element is a peak element.
It is clear from above examples that there is always a peak element in the input array.
A simple solution is to do a linear scan of array and as soon as we find a peak element, we return it. The worst case time complexity of this method would be O(n).
Can we find a peak element in worst time complexity better than O(n)?
We can use Divide and Conquer to find a peak in O(Logn) time. The idea is Binary Search based, we compare middle element with its neighbors. If middle element is not smaller than any of its neighbors, then we return it. If the middle element is smaller than the its left neighbor, then there is always a peak in left half (Why? take few examples). If the middle element is smaller than the its right neighbor, then there is always a peak in right half (due to same reason as left half). Following are C and Java implementations of this approach.
C/C++
// A C++ program to find a peak element element using divide and conquer #include <stdio.h> // A binary search based function that returns index of a peak element int findPeakUtil(int arr[], int low, int high, int n) { // Find index of middle element int mid = low + (high - low)/2; /* (low + high)/2 */ // Compare middle element with its neighbours (if neighbours exist) if ((mid == 0 || arr[mid-1] <= arr[mid]) && (mid == n-1 || arr[mid+1] <= arr[mid])) return mid; // If middle element is not peak and its left neighbour is greater // than it, then left half must have a peak element else if (mid > 0 && arr[mid-1] > arr[mid]) return findPeakUtil(arr, low, (mid -1), n); // If middle element is not peak and its right neighbour is greater // than it, then right half must have a peak element else return findPeakUtil(arr, (mid + 1), high, n); } // A wrapper over recursive function findPeakUtil() int findPeak(int arr[], int n) { return findPeakUtil(arr, 0, n-1, n); } /* Driver program to check above functions */int main() { int arr[] = {1, 3, 20, 4, 1, 0}; int n = sizeof(arr)/sizeof(arr[0]); printf("Index of a peak point is %d", findPeak(arr, n)); return 0; } |
Java
// A Java program to find a peak element element using divide and conquer import java.util.*; import java.lang.*; import java.io.*; class PeakElement { // A binary search based function that returns index of a peak // element static int findPeakUtil(int arr[], int low, int high, int n) { // Find index of middle element int mid = low + (high - low)/2; /* (low + high)/2 */ // Compare middle element with its neighbours (if neighbours // exist) if ((mid == 0 || arr[mid-1] <= arr[mid]) && (mid == n-1 || arr[mid+1] <= arr[mid])) return mid; // If middle element is not peak and its left neighbor is // greater than it,then left half must have a peak element else if (mid > 0 && arr[mid-1] > arr[mid]) return findPeakUtil(arr, low, (mid -1), n); // If middle element is not peak and its right neighbor // is greater than it, then right half must have a peak // element else return findPeakUtil(arr, (mid + 1), high, n); } // A wrapper over recursive function findPeakUtil() static int findPeak(int arr[], int n) { return findPeakUtil(arr, 0, n-1, n); } // Driver method public static void main (String[] args) { int arr[] = {1, 3, 20, 4, 1, 0}; int n = arr.length; System.out.println("Index of a peak point is " + findPeak(arr, n)); } } |
Python3
# A python 3 program to find a peak # element element using divide and conquer # A binary search based function # that returns index of a peak element def findPeakUtil(arr, low, high, n): # Find index of middle element # (low + high)/2 mid = low + (high - low)/2 mid = int(mid) # Compare middle element with its # neighbours (if neighbours exist) if ((mid == 0 or arr[mid - 1] <= arr[mid]) and (mid == n - 1 or arr[mid + 1] <= arr[mid])): return mid # If middle element is not peak and # its left neighbour is greater # than it, then left half must # have a peak element elif (mid > 0 and arr[mid - 1] > arr[mid]): return findPeakUtil(arr, low, (mid - 1), n) # If middle element is not peak and # its right neighbour is greater # than it, then right half must # have a peak element else: return findPeakUtil(arr, (mid + 1), high, n) # A wrapper over recursive # function findPeakUtil() def findPeak(arr, n): return findPeakUtil(arr, 0, n - 1, n) # Driver code arr = [1, 3, 20, 4, 1, 0] n = len(arr) print("Index of a peak point is", findPeak(arr, n)) # This code is contributed by # Smitha Dinesh Semwal |
C#
// A C# program to find // a peak element element // using divide and conquer using System; class GFG { // A binary search based // function that returns // index of a peak element static int findPeakUtil(int []arr, int low, int high, int n) { // Find index of // middle element int mid = low + (high - low) / 2; // Compare middle element with // its neighbours (if neighbours // exist) if ((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid])) return mid; // If middle element is not // peak and its left neighbor // is greater than it,then // left half must have a // peak element else if (mid > 0 && arr[mid - 1] > arr[mid]) return findPeakUtil(arr, low, (mid - 1), n); // If middle element is not // peak and its right neighbor // is greater than it, then // right half must have a peak // element else return findPeakUtil(arr, (mid + 1), high, n); } // A wrapper over recursive // function findPeakUtil() static int findPeak(int []arr, int n) { return findPeakUtil(arr, 0, n - 1, n); } // Driver Code static public void Main () { int []arr = {1, 3, 20, 4, 1, 0}; int n = arr.Length; Console.WriteLine("Index of a peak " + "point is " + findPeak(arr, n)); } } // This code is contributed by ajit |
PHP
<?php // A PHP program to find a // peak element element using // divide and conquer // A binary search based function // that returns index of a peak // element function findPeakUtil($arr, $low, $high, $n) { // Find index of middle element $mid = $low + ($high - $low) / 2; // (low + high)/2 // Compare middle element with // its neighbours (if neighbours exist) if (($mid == 0 || $arr[$mid - 1] <= $arr[$mid]) && ($mid == $n - 1 || $arr[$mid + 1] <= $arr[$mid])) return $mid; // If middle element is not peak // and its left neighbour is greater // than it, then left half must // have a peak element else if ($mid > 0 && $arr[$mid - 1] > $arr[$mid]) return findPeakUtil($arr, $low, ($mid - 1), $n); // If middle element is not peak // and its right neighbour is // greater than it, then right // half must have a peak element else return(findPeakUtil($arr, ($mid + 1), $high, $n)); } // A wrapper over recursive // function findPeakUtil() function findPeak($arr, $n) { return floor(findPeakUtil($arr, 0, $n - 1, $n)); } // Driver Code $arr = array(1, 3, 20, 4, 1, 0); $n = sizeof($arr); echo "Index of a peak point is ", findPeak($arr, $n); // This code is contributed by ajit ?> |
Output :
Index of a peak point is 2
Time Complexity: O(Logn) where n is number of elements in input array.
Exercise:
Consider the following modified definition of peak element. An array element is peak if it is greater than its neighbors. Note that an array may not contain a peak element with this modified definition.
References:
http://courses.csail.mit.edu/6.006/spring11/lectures/lec02.pdf
http://www.youtube.com/watch?v=HtSuA80QTyo
Asked in: Amazon
Related Problem:
Find local minima in an array
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find a peak element in a 2D array
- Find last element after deleting every second element in array of n integers
- Find the element that appears once in an array where every other element appears twice
- Find the element that appears once
- Find three element from given three arrays such that their sum is X | Set 2
- Find the only repetitive element between 1 to n-1
- Find three element from different three arrays such that that a + b + c = sum
- Find maximum element of each row in a matrix
- Find the Majority Element | Set 3 (Bit Magic)
- Find the only element that appears b times
- Find an element in Bitonic array
- Find k-th smallest element in given n ranges
- Find closest value for every element in array
- Find the odd appearing element in O(Log n) time
- Find Second largest element in an array



