Find Peak Element šŸŽÆ

beginner
5 min

Find Peak Element šŸŽÆ

Welcome to CodeYourCraft, where we learn to code together! Today, we're going to dive into an exciting topic called Find Peak Element. This concept is essential for understanding and solving various problems related to data structures and algorithms. Let's get started!

What is a Peak Element? šŸ“

A peak element in an array is an element that is greater than its neighboring elements on both sides (if such elements exist). In other words, a peak element is the highest point in a mountain range (or array) represented by numbers.

Here's a simple example:

python
arr = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]

In the above array, the peak element is 5, as it's higher than the elements on both sides (assuming we consider the first and last elements as neighbors).

Algorithm for Finding Peak Element šŸ’”

The algorithm to find the peak element is quite straightforward. Let's go through the steps together:

  1. Iterate through the array from left to right.
  2. If the current element is greater than the previous element, then the current element could be a peak. Remember this position for later.
  3. If the current element is less than the previous element, then we know that the previous element is the peak element (as the current element is lower).
  4. If we reach the end of the array, the last element is the peak element.
  5. If we find more than one peak, the highest peak will be the final answer.

Let's put this algorithm into practice with an example:

python
def findPeak(arr): # Initialize the peak index peak = len(arr) - 1 # Iterate through the array, starting from the second element for i in range(1, len(arr)): if arr[i] > arr[peak]: # If the current element is higher, update the peak index peak = i return arr[peak] arr = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1] print(findPeak(arr)) # Output: 5

In this example, we defined a findPeak function that takes an array as an argument and returns the peak element. The function iterates through the array and compares each element with the current peak element. If it finds a higher element, it updates the peak index accordingly.

Advanced Example šŸ’”

Let's consider an advanced example where the array may have multiple peaks and we need to find the highest peak:

python
arr = [2, 1, 2, 3, 4, 7, 10, 9, 8, 7, 6, 5, 4, 3, 2] def findPeak(arr): # Initialize the peak index and maximum height peak = 0 max_height = arr[0] # Iterate through the array, starting from the second element for i in range(1, len(arr)): if arr[i] > max_height: # If the current element is higher, update the maximum height and peak index max_height = arr[i] peak = i return arr[peak] print(findPeak(arr)) # Output: 10

In this example, we defined a similar findPeak function, but this time we initialize a variable to store the maximum height found so far. This allows us to handle multiple peaks in the array.

Quiz Time šŸŽÆ

Quick Quiz
Question 1 of 1

What is a peak element in an array?

That's all for today! I hope you found the lesson on finding peak elements interesting and informative. Practice makes perfect, so feel free to try implementing this algorithm on your own or modify it to fit different scenarios. Happy coding! 😃