## Peak element in the Array in CIn 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.
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
- Check if the first element of the Array is greater than the second element. If yes, return 0.
- Check if the last element of the Array is greater than the last but one element. If yes, return n-1.
- 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.
Enter the size of the Array: 5 Enter the elements: 8 2 3 9 5 The first Peak element index in the Array: 0
## The Naïve approach #2: Finding the largest element in the ArrayIf 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.
- Use two variables, max = arr[0] and maxindex = 0
- 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.
- Finally, return maxindex.
Enter size of the Array: 5 Enter the elements: 8 2 3 9 5 Peak element index in the Array: 3
## 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
- Initialize l = 0 and h = n - 1. Find the mid element: mid = l + (h - l) /2.
- Check if the mid element is a Peak value. If yes, return it.
- Check if the element on the left side of the mid element is greater than the mid element. If yes, update h = mid - 1.
- Check if the element on the right side of the mid element is greater than the mid element. If yes, update l = mid + 1
Enter size of the Array: 5 Enter the elements: 8 2 3 9 5 Peak element index in the Array: 3
## 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.
Enter size of the Array: 5 Enter the elements: 8 2 3 9 5 Peak element index in the Array: 3 |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]