Peak element in the Array in C

In an Array, the Peak element is the element that is not smaller than its neighbors. It is one of the frequently asked questions in interviews. It is simple. This tutorial shows different approaches we can follow to solve the question efficiently.

Example:

Suppose, in the Array [10, 20, 15, 23, 14]:

23 is the peak element as it is not smaller than both of its neighbors-15 and 14.

  • If the given input array is sorted in ascending order, the last element will be the Peak element.
  • If the Array is sorted in descending order, the first element will be the Peak element. If all the elements in the Array are equal, then all the elements are Peak elements.
  • This confirms that there will always be at least one peak element in every Array.

The Naïve approach #1: Traversing the whole Array

Algorithm:

  1. Check if the first element of the Array is greater than the second element. If yes, return 0.
  2. Check if the last element of the Array is greater than the last but one element. If yes, return n-1.
  3. Iterate an integer i in the Array from index 1 to n-1 and check if arr[i] > arr[i - 1] and arr[i] < arr[i + 1]. If yes, return i.

Code:

Output:

Enter the size of the Array: 5
Enter the elements: 8 2 3 9 5
The first Peak element index in the Array: 0

Time complexity: O(n) => One traversal

The Naïve approach #2: Finding the largest element in the Array

If we find the largest element in the Array, automatically, its neighbors will be smaller than the element. We can use the max() function on the Array.

Algorithm:

  1. Use two variables, max = arr[0] and maxindex = 0
  2. Iterate an integer i from the second element of the Array and keep checking if arr[i] > max. If yes, update the value of max = arr[i] and maxindex = i.
  3. Finally, return maxindex.

Code:

Output:

Enter size of the Array: 5
Enter the elements: 8 2 3 9 5
Peak element index in the Array: 3

Time complexity: O(n) -> One traversal

Efficient approach:

We saw above that finding the largest element got us one peak element. All the time is taken away to travel through the whole Array, finding the maximum of all the elements. We can break the Array into smaller parts and find the peak element in those parts.

Hence, we use the "Divide and conquer" approach. We'll follow the algorithm of Binary search.

Algorithm:

  1. Initialize l = 0 and h = n - 1. Find the mid element: mid = l + (h - l) /2.
  2. Check if the mid element is a Peak value. If yes, return it.
  3. Check if the element on the left side of the mid element is greater than the mid element. If yes, update h = mid - 1.
  4. Check if the element on the right side of the mid element is greater than the mid element. If yes, update l = mid + 1

Code:

Output:

Enter size of the Array: 5
Enter the elements: 8 2 3 9 5
Peak element index in the Array: 3

Time complexity: O(Logn) -> Each step, array breaks into half -> Binary search

The same code in the Iterative approach:

Why do we have to use this approach without the recursion? Both codes are the same, but when we use recursion, an implicit stack is used to keep track of the recursive calls; we can eliminate it using loops. Hence, using this approach the Time complexity isn't changed but space complexity becomes constant.

Code:

Output:

Enter size of the Array: 5
Enter the elements: 8 2 3 9 5
Peak element index in the Array: 3





Latest Courses