โ† Back to All Algorithms

Medium Interview Frequency: โญโญโญ

Find Peak Element

A peak is an index i where nums[i] > nums[i-1] and nums[i] > nums[i+1] (out-of-bounds neighbors treated as negative infinity). Return any peak index.

Binary Search Array Peaks

๐Ÿ“š Table of Contents

  1. Problem Statement
  2. Real-World Analogy
  3. Key Insights
  4. Solution Approaches
  5. Step-by-Step Walkthrough
  6. Complexity Analysis
  7. Edge Cases
  8. Test Cases
  9. Interview Tips
  10. Related Problems
  11. Key Takeaways

๐Ÿ“‹ Problem Statement

Find an index that is strictly greater than its immediate neighbors. Any valid peak is acceptable.

Examples

Example 1:
Input: nums = [1,2,3,1]
Output: 2
Example 2:
Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5

Constraints

๐ŸŽฏ Real-World Analogy

Hiking Toward a Summit

If the trail slopes upward from mid to mid+1, a peak must exist to the right. If it slopes downward or flat-left direction, a peak exists on the left side.

Binary search keeps only the side guaranteed to contain at least one summit.

๐Ÿ’ก Key Insights

๐Ÿ’ป Solution Approaches

Approach 1: Linear Scan

Time: O(N) | Space: O(1)

Find first drop and return previous element index.

Approach 2: Binary Search on Slope (Recommended)

Time: O(log N) | Space: O(1)

Use slope between mid and mid+1 to discard half of search space.

Python Implementation

from typing import List def find_peak_element(nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] < nums[mid + 1]: left = mid + 1 else: right = mid return left

JavaScript Implementation

function findPeakElement(nums) { let left = 0; let right = nums.length - 1; while (left < right) { const mid = Math.floor((left + right) / 2); if (nums[mid] < nums[mid + 1]) { left = mid + 1; } else { right = mid; } } return left; }

๐Ÿ” Step-by-Step Walkthrough

Trace [1,2,1,3,5,6,4]:

left=0 right=6 mid=3. Compare 3 and 5 -> ascending -> left=4.
left=4 right=6 mid=5. Compare 6 and 4 -> descending -> right=5.
left=4 right=5 mid=4. Compare 5 and 6 -> ascending -> left=5.
left == right == 5. Peak index is 5.

โšก Complexity Analysis

Time Complexity: O(log N)

Space Complexity: O(1)

๐Ÿ”ง Edge Cases

๐Ÿงช Test Cases

Python

def is_peak(nums, i): left = nums[i - 1] if i - 1 >= 0 else float('-inf') right = nums[i + 1] if i + 1 < len(nums) else float('-inf') return nums[i] > left and nums[i] > right def test_find_peak_element(): i1 = find_peak_element([1,2,3,1]) assert i1 == 2 arr = [1,2,1,3,5,6,4] i2 = find_peak_element(arr) assert is_peak(arr, i2) assert find_peak_element([1]) == 0 assert find_peak_element([2,1]) == 0 print("โœ… all tests passed")

JavaScript

function testFindPeakElement() { console.assert(findPeakElement([1,2,3,1]) === 2, "test 1"); const i = findPeakElement([1,2,1,3,5,6,4]); console.assert([1,5].includes(i), "test 2"); console.assert(findPeakElement([1]) === 0, "test 3"); console.log("โœ… all tests passed"); }

๐Ÿ’ผ Interview Tips

๐ŸŽ“ Key Takeaways

โ† Back to All Algorithms