Linearity of expectation — always try this first

regardless of dependence between the . This is the most useful fact in probabilistic problem solving, because it lets you decompose a complicated random quantity into simple indicators.

Worked examples

QuestionDecompositionAnswer
Expected fixed points of a random permutation, each
Expected inversions,
Expected distinct values among uniform draws from
Expected length of the longest runharder — linearity gives bounds, not the exact value
Expected number of records (“new maximum so far”)
Expected cycles in a random permutationsame as records
Expected edges in a random subgraph each edge survives
Expected connected componentsharder — needs more than linearity

The records result is a good one to remember: element is the maximum of the first with probability exactly , by symmetry.

Indicator variables — the technique

  1. Write the quantity as a sum of 0/1 indicators.
  2. Compute for each, using symmetry wherever possible.
  3. Add them up.

Step 2 is where symmetry does the work: “by symmetry, each of the positions is equally likely to be the maximum” replaces a calculation with an observation.

Contribution technique

The deterministic cousin, and just as useful. Instead of summing over configurations, sum over elements, counting how many configurations each contributes to:

ProblemContribution
Sum of over all subarrayseach element × the subarrays where it is the minimum (monotonic stack)
Sum of all pairwise tree distanceseach edge ×
Sum of over all pairseach × the number of pairs with that gcd
Sum over all subsets of the subset’s XOReach bit, independently
Expected value of a max

That last identity is the tail-sum formula: for a non-negative integer random variable,

It converts “expected maximum” — which linearity cannot handle — into a sum of probabilities, which it can.

Conditional expectation

Condition on the first step (giving a recurrence for expected time), or on a hidden variable (the value of the maximum, the position of a pivot).

The law of total expectation is what turns “expected number of steps” into the backward DP of Probability DP.

Classic results

ProblemAnswer
Expected trials until success, probability
Coupon collector
Expected comparisons in randomized quicksort
Expected depth of a random BST / treap
Expected height of a random BST
Random walk on from : reach before 0probability ; expected time
Expected maximum of uniform
Expected minimum of uniform
Expected number of local maxima in a random permutation (interior positions have )
Birthday paradoxcollision after draws

Modular expected values

Most contest problems now ask for the answer modulo . Represent every probability as and work exactly — no floating point anywhere. This removes all precision concerns and is why “output ” has become the standard phrasing.

Pitfalls

Three traps

  1. unless and are independent. Linearity has no such requirement; products do.
  2. . Ever.
  3. — use the tail-sum formula instead.

See also: Probability · Probability DP · Randomized Algorithms