Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find Peak Element in Python
A peak element in an array is defined as an element that is greater than its neighbors. We can use some common approaches such as binary search to efficiently locate a peak element and linear search algorithm which involves iterating through an array.
What is a Peak Element?
A peak element is an element that is greater than or equal to its adjacent neighbors. For elements at the edges of the array, we only consider the existing neighbor ?
# Example array with peak elements
nums = [1, 3, 20, 4, 1, 0]
# Here, 20 is a peak element (20 > 3 and 20 > 4)
print("Array:", nums)
print("Peak element 20 at index 2")
Array: [1, 3, 20, 4, 1, 0] Peak element 20 at index 2
Binary Search Approach
The Binary Search approach provides an efficient O(log n) solution. The key insight is that we can always find a peak by moving toward the larger neighbor ?
Algorithm Steps
-
Choose the middle element of the array
-
If it's greater than or equal to the left neighbor, search the right half
-
Otherwise, search the left half
-
Repeat until a peak is found
Implementation
class Solution:
def findPeakElement(self, nums):
low = 0
high = len(nums) - 1
while low < high:
mid = low + (high - low + 1) // 2
# Check if mid is greater than or equal to its left neighbor
if mid - 1 >= 0 and nums[mid - 1] <= nums[mid]:
low = mid
else:
high = mid - 1
return low
# Example usage
solution = Solution()
nums = [15, 35, 85, 96, 5, 6, 8, 12]
result = solution.findPeakElement(nums)
print(f"Array: {nums}")
print(f"Peak element {nums[result]} found at index: {result}")
Array: [15, 35, 85, 96, 5, 6, 8, 12] Peak element 96 found at index: 3
Linear Search Approach
The linear search approach iterates through the array to identify a peak element. This method has O(n) time complexity but is simpler to understand ?
Implementation
class Solution:
def findPeakElement(self, nums):
n = len(nums)
for i in range(n):
# Check if current element is greater than its neighbors
left_ok = (i == 0) or (nums[i] >= nums[i - 1])
right_ok = (i == n - 1) or (nums[i] >= nums[i + 1])
if left_ok and right_ok:
return i
return 0 # Fallback (should not reach here with valid input)
# Example usage
solution = Solution()
nums = [15, 35, 85, 96, 5, 6, 8, 12]
result = solution.findPeakElement(nums)
print(f"Array: {nums}")
print(f"Peak element {nums[result]} found at index: {result}")
Array: [15, 35, 85, 96, 5, 6, 8, 12] Peak element 35 found at index: 1
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Binary Search | O(log n) | O(1) | Large arrays |
| Linear Search | O(n) | O(1) | Small arrays, simple logic |
Edge Cases
solution = Solution()
# Single element array
single = [5]
print(f"Single element {single}: peak at index {solution.findPeakElement(single)}")
# Ascending array
ascending = [1, 2, 3, 4, 5]
result = solution.findPeakElement(ascending)
print(f"Ascending {ascending}: peak at index {result}")
# Descending array
descending = [5, 4, 3, 2, 1]
result = solution.findPeakElement(descending)
print(f"Descending {descending}: peak at index {result}")
Single element [5]: peak at index 0 Ascending [1, 2, 3, 4, 5]: peak at index 4 Descending [5, 4, 3, 2, 1]: peak at index 0
Conclusion
Binary search provides the most efficient O(log n) solution for finding peak elements in large arrays. Linear search offers a simpler O(n) approach suitable for small datasets or when simplicity is preferred over efficiency.
