162. Find Peak Element - Detailed Explanation

Problem statement

Given an array of integers nums, a peak element is an element that is strictly greater than its neighbors. You need to find a peak element and return its index. If the array contains several peaks, the index of any one of them is acceptable.

Additional details:

  • You may assume that nums[-1] and nums[n] are negative infinity, where n is the length of the array. Those positions do not exist, and treating them as negative infinity guarantees that at least one peak exists.
  • The input array can hold a single element, which is trivially a peak.

Example inputs, outputs, and explanations

  1. Example 1:

    • Input: nums = [1,2,3,1].
    • Output: 2.
    • Explanation: The element at index 2 is 3, which is greater than its neighbors 2 and 1.
  2. Example 2:

    • Input: nums = [1,2,1,3,5,6,4].
    • Output: Could be 1 or 5.
    • Explanation:
      • At index 1, the element is 2, with neighbors 1 and 1, so 2 is a peak.
      • At index 5, the element is 6, with neighbors 5 and 4, so 6 is also a peak.
  3. Example 3 (edge case):

    • Input: nums = [1].
    • Output: 0.
    • Explanation: With only one element, it is a peak by definition.

Constraints

  • The length of nums is at least 1.
  • You can assume that nums[i] is not equal to nums[i+1] for all valid indices, which rules out plateaus.
  • An optimal solution is expected to run faster than O(n), ideally in O(log n).

This question is an instance of the modified binary search pattern. Classic binary search needs a sorted array, while a modified binary search keeps the halving step and swaps in a different test for deciding which half to keep. Here the test is a single comparison between a neighbor pair. The pattern applies well beyond this one problem, and the summary of the most common LeetCode patterns shows how often it appears.

Hints to approach the solution

  1. Directional clues from neighbors:

    • If an element is less than its right neighbor, a peak has to exist on the right side. The values are climbing, and the climb cannot continue past the end of the array.
    • If an element is greater than its right neighbor, a peak has to lie on the left, and it may be the current element itself.
  2. Binary search opportunity:

    • Given that observation, you can compare the middle element with its neighbor and decide which half of the array still contains a peak. That decision discards half the candidates per step.

Approach 1: linear scan (brute force)

Explanation

  • Method:
    • Traverse the array from left to right and test each element for the peak property.
    • For each index i, verify that nums[i] is greater than both nums[i-1], when i > 0, and nums[i+1], when i < n-1.
  • Edge handling:
    • Handle the first and last elements separately, since each has only one neighbor.

Pseudocode

if length(nums) == 1: return 0 if nums[0] > nums[1]: return 0 if nums[n-1] > nums[n-2]: return n-1 for i in range(1, n-1): if nums[i] > nums[i-1] and nums[i] > nums[i+1]: return i

Downsides:

  • This approach runs in O(n) time. That is acceptable for small inputs, but it does not meet the O(log n) target the problem sets.

Approach 2: binary search (optimal)

Explanation

  • Method:
    • Use binary search to shrink the search space.
    • At each iteration, compare the middle element with its right neighbor.
      • If nums[mid] > nums[mid+1], a peak exists in the left half, including mid itself.
      • Otherwise, a peak exists in the right half.
    • Continue until the search space narrows to one element, which is a peak.
  • Why it works:
    • Peaks are defined by strict inequality, and the boundaries nums[-1] and nums[n] count as negative infinity. Together those two facts mean the half you keep always still contains a peak, so binary search can move toward one safely.

Pseudocode

left = 0, right = n - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[mid + 1]: right = mid else: left = mid + 1 return left

Detailed walkthrough (visual example)

Consider the input nums = [1,2,3,1]:

StepleftrightmidComparisonDecision
Initialization03n/an/an/a
Iteration 1031Compare 2 and 3Since 2 < 3, set left = mid + 1, so left becomes 2
Iteration 2232Compare 3 and 1Since 3 > 1, set right = mid, so right becomes 2
Termination22n/an/aReturn left, index 2

The element at index 2 is 3, which is a peak.

Code implementations

Python code

Python3
Python3

. . . .

Java code

Java
Java

. . . .

Complexity analysis

  • Time complexity:

    • Brute force (linear scan): O(n) in the worst case.
    • Binary search (optimal): O(log n), because the search space halves on every iteration.
  • Space complexity:

    • Both approaches use O(1) additional space.

Common mistakes and edge cases

Common mistakes

  • Boundary conditions:
    • Not handling the first and last elements correctly.
    • Off-by-one errors in the binary search bounds. Note that the loop keeps mid as a candidate when it sets right = mid, and excludes it when it sets left = mid + 1.
  • Incorrect assumptions:
    • Assuming the global maximum is required. Any local peak is an accepted answer.

Edge cases

  • Single element array:
    • The only element is automatically a peak.
  • Strictly increasing or decreasing array:
    • In a strictly increasing array, the last element is a peak.
    • In a strictly decreasing array, the first element is a peak.

Variations

  • Find all peak elements:
    • Instead of returning one peak, you might be asked for the indices of every peak in the array. Binary search no longer applies, since a full scan is the only way to report them all.
  • Finding valleys:
    • A variation asks for a valley, an element strictly less than its neighbors. Flip the comparison and the same halving argument holds.
  • Search in Rotated Sorted Array: Another array that is not sorted end to end, yet still yields to a halving decision. It appears on the Blind 75 list, which is a good place to see how many questions reduce to this pattern.
  • Koko Eating Bananas: The Koko Eating Bananas answer runs a binary search over an answer range rather than over array indices, which is the next step up in the same family.
  • Longest Increasing Subsequence: A dynamic programming question that shares the vocabulary of rising and falling runs, but not the technique.
  • Container With Most Water: A two pointers question, useful as a contrast with the halving decision used here.

The course Grokking the Coding Interview teaches modified binary search alongside the other patterns, including the variants where the array is rotated or the search bounds are unknown.

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Which industry has the most remote workers?
How long does coding bootcamp take?
Will technical writers be replaced by AI?
What is CRM in Salesforce interview questions?
How to prepare for coding interviews in assembly language?
What Is the Shopify Interview Process Like? (Round by Round)
Shopify's loop is built around two signatures: the Life Story interview and AI-tools-welcome pair programming. The full structure, timelines, and how to prepare for each stage.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.