Skip to content

Find Peak Element

A peak element is an element that is strictly greater than its neighbors. Given an integer array nums, find a peak element, and return its index. If the array has multiple peaks, return the index to any one of them.

You may imagine that nums[-1] = nums[n] = -infinity.

You must write an algorithm that runs in O(log n) time.

InputOutputExplanation
[1, 2, 3, 1]23 is a peak (3 > 2 and 3 > 1)
[1, 2, 1]12 is a peak (2 > 1 and 2 > 1)
[1]0Single element is always a peak
[6, 5, 4, 3, 2, 3, 4]06 is a peak (6 > 5)
  • 1 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums[i] != nums[i + 1] for all valid i
ApproachTimeSpaceBest When
Binary SearchO(log n)O(1)Large arrays, O(log n) required by constraints
Linear ScanO(n)O(1)Small arrays, simplicity preferred
  • Pick Binary Search to meet the O(log n) constraint and understand the pattern.
  • Pick Linear Scan to understand how peaks work conceptually.
Required Time
Binary Search
O(log n) time · O(1) space
Simpler Logic
Linear Scan
O(n) time · O(1) space
★ Recommended

Compare nums[mid] with nums[mid + 1]. If mid is less than its right neighbor, the peak must be on the right (since there’s an increasing trend). Otherwise, search left. This guarantees finding a peak.

⏱ Time O(log n) Halve search space each iteration 💾 Space O(1) Only pointers

Input: nums = [1, 2, 3, 1]

Stepleftrightmidnums[mid]nums[mid+1]nums[mid] < nums[mid+1]?Action
103123Yesleft = 2
223231Noright = 2
322Return 2
function findPeakElement(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) / 2
if nums[mid] < nums[mid + 1]:
left = mid + 1 // Peak on right
else:
right = mid // Peak on left or at mid
return left
find_peak_element_binary_search.py
"""
#162 Find Peak Element - Binary Search Approach
Time: O(log n), Space: O(1)
A peak element is an element that is strictly greater than its neighbors.
The array may contain multiple peaks; return index of any one.
"""
class Solution:
def findPeakElement(self, nums: list[int]) -> int:
"""
Binary search by comparing mid with its right neighbor.
If nums[mid] < nums[mid+1], peak is on the right.
Otherwise, peak is on the left or at mid.
"""
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) // 2
# If mid is less than its right neighbor, peak is on right
if nums[mid] < nums[mid + 1]:
left = mid + 1
else:
# Peak is on left or at mid
right = mid
return left
# Test cases
if __name__ == "__main__":
sol = Solution()
assert sol.findPeakElement([1, 2, 3, 1]) == 2
assert sol.findPeakElement([1, 2, 1]) == 1
assert sol.findPeakElement([1]) == 0
assert sol.findPeakElement([6, 5, 4, 3, 2, 3, 4]) == 0
assert sol.findPeakElement([1, 2, 3, 4, 5]) == 4
print("All test cases passed!")
✓ Simple

Iterate through the array and check if each element is greater than its neighbors. Return the first peak found.

⏱ Time O(n) Full array scan 💾 Space O(1) Only pointers

Input: nums = [1, 2, 3, 1]

inums[i]Left neighborRight neighborIs peak?Action
01-∞2No (1 < 2)Continue
1213No (2 < 3)Continue
2321Yes (3 > 2 && 3 > 1)Return 2
function findPeakElement(nums):
for i in range(len(nums)):
isLeft = (i == 0) or (nums[i] > nums[i-1])
isRight = (i == len(nums)-1) or (nums[i] > nums[i+1])
if isLeft and isRight:
return i
find_peak_element_linear_scan.py
"""
#162 Find Peak Element - Linear Scan Approach
Time: O(n), Space: O(1)
Iterate through array and find the first element greater than its neighbors.
"""
class Solution:
def findPeakElement(self, nums: list[int]) -> int:
"""
Linear scan to find first peak.
Compare each element with its neighbors.
"""
for i in range(len(nums)):
# Check if current element is greater than neighbors
is_peak = True
if i > 0 and nums[i] <= nums[i - 1]:
is_peak = False
if i < len(nums) - 1 and nums[i] <= nums[i + 1]:
is_peak = False
if is_peak:
return i
# If no peak found (shouldn't happen given constraints)
return 0
# Test cases
if __name__ == "__main__":
sol = Solution()
assert sol.findPeakElement([1, 2, 3, 1]) == 2
assert sol.findPeakElement([1, 2, 1]) == 1
assert sol.findPeakElement([1]) == 0
assert sol.findPeakElement([6, 5, 4, 3, 2, 3, 4]) == 0
assert sol.findPeakElement([1, 2, 3, 4, 5]) == 4
print("All test cases passed!")
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
find_peak_element_space_optimized.py
"""
Solution for Find Peak Element
"""
def solve():
"""Implementation for approach 3 of Find Peak Element"""
pass
if __name__ == "__main__":
print("Solution ready")