Local Maxima
Problem
pieces of paper labelled – are placed in a line at random. We spot is a local maxima if the paper at the th position is strictly larger than all of its adjacent papers. Find the expected number of local maxima in the sequence. For example, with numbers, has local maxima, at the first, third, and last spots.
Original Problem Link: https://www.quantguide.io/questions/local-maxima
Solution
Counting local maxima directly, by cases on the actual arrangement, looks painful. But every permutation of is equally likely, and that symmetry lets the whole thing collapse.
Step 1: Set up with linearity of expectation
For position , let if is a local maximum and otherwise. We want
Each only depends on or nearby values, so we just need for an edge position and for a middle position. By symmetry, that probability is the same for every edge position, and the same for every middle position.
Step 2: Edge positions
Position (and symmetrically, position ) has only one neighbor, so it is a local max exactly when it beats that single neighbor. Since all permutations are equally likely, this is the same as picking random distinct numbers and asking . By symmetry between and , that's
Step 3: Middle positions
A middle position has two neighbors, and is a local max exactly when it beats both. Equivalently: pick random distinct numbers (standing for the left neighbor, the position itself, and the right neighbor): what's , i.e. ?
All orderings of are equally likely, and is the largest in exactly of them ( and ). So
Step 4: Put it together
There are edge positions and middle positions, and each contributes its probability independently to the sum in Step 1:
For :
A slower approach, for fun: recursion
It's worth seeing fall out of a recursion too, by relating to directly, since it makes the "why" more concrete.
Building a random permutation of size from one of size . Take a uniformly random permutation of , and insert the new largest value into one of the possible gaps (before the first element, between two elements, or after the last), each gap equally likely. This produces a uniformly random permutation of .
What inserting does to the maxima count. Since is the biggest value around, wherever it lands it is automatically a new local maximum: that's a guaranteed . But it can also destroy existing maxima, because any old element now adjacent to picks up a neighbor bigger than itself, and can no longer be a local max:
- If lands at an edge (probability ), it touches exactly old position, so it can destroy at most that old maximum.
- If lands in the middle (probability ), it touches old positions (the ones that used to be adjacent to each other), so it can destroy up to old maxima.
Now take expectations. Every old position is, on average, a local max with probability (that's just the average over all positions). So the expected number of old maxima destroyed is per touched position, and every old maximum that isn't touched survives unchanged, contributing its share to as usual. Putting the surviving part, the destroyed part, and the guaranteed new maximum together:
The bracket simplifies nicely:
so the recursion collapses to
with (with only elements, both are edges, and exactly one of them is bigger, so there's always exactly local max). Unrolling this recursion (or just checking that satisfies it) reproduces:
exactly matching Step 4.
A closing note: why and never drift
The per-position probabilities (edge) and (middle), computed at the small case , keep working unchanged all the way out to , and this isn't a coincidence. We can set up a recurrence for these probabilities themselves (in the same spirit as the recursion for above), and it turns out the base case is exactly a fixed point of that recurrence, which is precisely why it never drifts, and gives a second, alternate lens on why the symmetry argument in Steps 2–3 works. I'll write a short blog post soon going into this properly.