peak finding
algorithm
computational mathematics
data analysis
computer science

Peak finding algorithm

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A peak is an element that is not smaller than its neighbors. Peak-finding matters in signal analysis, optimization, image processing, and algorithm interviews because you often do not need the global maximum; you only need some locally maximal point that satisfies the peak condition.

Peak Finding in One Dimension

For a one-dimensional array, an element A[i] is a peak if it is at least as large as its immediate neighbors. At the edges, only one neighbor exists.

A simple linear algorithm checks each position in order.

python
1def find_peak_linear(values):
2    n = len(values)
3    if n == 0:
4        raise ValueError("array must not be empty")
5    if n == 1 or values[0] >= values[1]:
6        return 0
7
8    for i in range(1, n - 1):
9        if values[i] >= values[i - 1] and values[i] >= values[i + 1]:
10            return i
11
12    return n - 1
13
14print(find_peak_linear([1, 3, 4, 1, 0]))

This runs in O(n) time and is easy to reason about.

A Faster Divide-and-Conquer Approach

In one dimension, you can find a peak in O(log n) time with a binary-search-like method.

The idea is:

  • inspect the middle element
  • if it is a peak, return it
  • if the left neighbor is larger, a peak must exist on the left side
  • otherwise a peak must exist on the right side
python
1def find_peak_binary(values):
2    left, right = 0, len(values) - 1
3
4    while left < right:
5        mid = (left + right) // 2
6        if values[mid] < values[mid + 1]:
7            left = mid + 1
8        else:
9            right = mid
10
11    return left
12
13print(find_peak_binary([1, 3, 4, 1, 0]))

This is a standard interview algorithm because it shows how local structure can support logarithmic search even when the array is not sorted.

Why the Binary Strategy Works

If values[mid] < values[mid + 1], then moving right goes uphill. A peak must exist somewhere on that side because the ascent must eventually stop or reach the edge, and either outcome creates a peak.

The symmetric argument applies when the left side is at least as large. That is what makes divide-and-conquer possible.

Two-Dimensional Peak Finding

In two dimensions, the problem is usually defined over a matrix where a peak is an element at least as large as its north, south, east, and west neighbors.

A straightforward method is to scan the whole matrix, but more interesting algorithms repeatedly choose a middle column, find that column’s maximum, and move toward a larger neighboring column if necessary.

The exact implementation is more involved than the one-dimensional case, but the same principle remains: follow the direction that proves a peak must exist.

When “Peak” Does Not Mean “Highest”

A common source of confusion is expecting the peak-finding algorithm to return the global maximum. It does not have to.

For example, in the array:

text
[1, 3, 2, 5, 4]

Both 3 and 5 are peaks. A correct algorithm may return either one depending on its strategy.

That distinction is important because local peak-finding is often much cheaper than computing the global maximum under stronger constraints.

Applications

Peak-finding ideas appear in:

  • time-series event detection
  • image feature extraction
  • hill-climbing style optimization
  • sensor or waveform analysis

The exact domain changes, but the core question remains the same: where does the signal stop rising locally.

Common Pitfalls

The most common mistake is confusing a local peak with the global maximum.

Another common issue is forgetting boundary cases at the start and end of the array. Developers also often assume the divide-and-conquer method needs sorted input, even though it relies only on local comparisons and the existence argument for peaks.

Summary

  • A peak is an element not smaller than its neighbors.
  • A linear scan finds a one-dimensional peak in O(n) time.
  • A binary-search-style method can find one in O(log n) time.
  • In two dimensions, similar ideas apply with column or row-based divide-and-conquer.
  • Peak-finding returns a valid local peak, not necessarily the global maximum.

Course illustration
Course illustration

All Rights Reserved.