VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-a-peak-in-a-given-array/

⇱ Peak Element in Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Peak Element in Array

Last Updated : 19 Jul, 2025

Given an array arr[] where no two adjacent elements are same, find the index of a peak element. An element is considered to be a peak element if it is strictly greater than its adjacent elements. If there are multiple peak elements, return the index of any one of them.

Note: Consider the element before the first element and the element after the last element to be negative infinity.

Examples:

Input: arr[] = [1, 2, 4, 5, 7, 8, 3]
Output: 5
Explanation: arr[5] = 8 is a peak element because arr[4] < arr[5] > arr[6].

Input: arr[] = [10, 20, 15, 2, 23, 90, 80]
Output: 1
Explanation: Element 20 at index 1 is a peak since 10 < 20 > 15. Index 5 (value 90) is also a peak, but returning any one peak index is valid.

[Naive Approach] Using Linear Search - O(n) Time and O(1) Space

The simplest approach is to iterate through the array and check if an element is greater than its neighbors. If it is, then it's a peak element.


Output
5

[Expected Approach] Using Binary Search - O(log n) Time and O(1) Space

If we observe carefully, we can say that:

If an element is smaller than it's next element then it is guaranteed that at least one peak element will exist on the right side of this element.

Conversely if an element is smaller than it's previous element then it is guaranteed that at least one peak element will exist on the left side of this element.

Therefore, we can use binary search to find the peak element.

Why it is guaranteed that peak element will definitely exist on the right side of an element, if its next element is greater than it?

If we keep moving in the right side of this element, as long as the elements are increasing, we will eventually reach an element that is either:

  • The last element of the array, which will be a peak as it is greater than or equal to its previous element.
  • An element where the sequence is no longer increasing, i.e., arr[i] > arr[i + 1], which would be a peak element.

For the same reasons, if an element is lesser than its previous element, then it is guaranteed that at least one peak element will exist on the left side of that element.


Output
5

Related Articles:

Comment