Skip to content
LeetQuant
F2 Counting and Combinatorics

F2 · Counting and Combinatorics · Lesson 4 of 4

Pigeonhole, Symmetry and Invariants

Existence and impossibility arguments without counting everything: the pigeonhole principle, symmetry, parity and colouring invariants, and monovariants.

18 min

Answering without enumerating

Many interview puzzles ask "is it always possible?", "can this ever happen?" or "what is the answer, without computing?". Four tools answer most of them:

  • Pigeonhole: proves something must exist.
  • Symmetry: shows that outcomes come in classes of equal size, so a probability or expectation can be read off.
  • Invariants: a quantity a process never changes, which proves some states are unreachable.
  • Monovariants: a quantity that only moves one way, which proves a process stops.

The skill is choosing the right pigeonholes or the right invariant. Saying out loud "let me look for something that does not change" is itself a good signal in an interview.

The pigeonhole principle

If NN objects go into kk boxes, some box holds at least ⌈N/k⌉\lceil N/k \rceil. Equivalently, to guarantee some box has at least rr objects you need k(r−1)+1k(r - 1) + 1 of them: with one fewer, you can put exactly r−1r - 1 in every box.

The objects and boxes are rarely given. Typical choices for the boxes are remainders mod nn, pairs that partition a set, or labels you attach to each object.

Worked example

a block sum divisible by n

Prove that among any nn integers a1,…,ana_1, \dots, a_n (not necessarily distinct), there is a non-empty block of consecutive ones whose sum is divisible by nn.

Show solution

Take the prefix sums S0=0S_0 = 0, S1=a1S_1 = a_1, S2=a1+a2S_2 = a_1 + a_2, …, Sn=a1+⋯+anS_n = a_1 + \cdots + a_n. That is n+1n + 1 numbers but only nn possible remainders mod nn, so two of them agree: Si≡Sj(modn)S_i \equiv S_j \pmod n with i<ji < j. Then

Sj−Si=ai+1+⋯+aj≡0(modn),S_j - S_i = a_{i+1} + \cdots + a_j \equiv 0 \pmod n ,

a non-empty consecutive block. Including S0=0S_0 = 0 is what makes nn numbers enough (rather than n+1n + 1).

The same trick ("prefix sums, then pigeonhole the remainders") also finds a subarray with zero sum, or one with sum divisible by kk, in linear time with a hash map.

Choosing the pigeonholes

Pairs. Choose any n+1n + 1 numbers from {1,…,2n}\{1, \dots, 2n\}. Partition the set into the nn pairs {1,2},{3,4},…\{1,2\}, \{3,4\}, \dots: two chosen numbers share a pair, so they are consecutive and hence coprime.

Odd parts. In the same setting, one chosen number divides another. Write each as 2km2^k m with mm odd. There are only nn odd numbers below 2n2n, so two chosen numbers share the same mm, and the one with the smaller power of 2 divides the other.

Labels (Erdős–Szekeres). Any sequence of n2+1n^2 + 1 distinct reals has an increasing or decreasing subsequence of length n+1n + 1. Label each term with (ik,dk)(i_k, d_k), the lengths of the longest increasing and decreasing subsequences ending at it. For j<kj < k, if aj<aka_j < a_k then ik>iji_k > i_j, otherwise dk>djd_k > d_j, so all labels are distinct. If every ik,dk≤ni_k, d_k \le n there would be at most n2n^2 labels for n2+1n^2 + 1 terms, a contradiction.

Symmetry

Symmetry turns a probability into a counting of equal classes. Two patterns are common.

  • Exchangeability: if relabelling objects does not change the distribution, they all have the same marginal behaviour. Each of the kk gaps that kk special items cut a random shuffle into has the same expected size.
  • Relabelling: if objects are indistinguishable, a complicated interaction can be replaced by a simple one. When two identical ants collide and reverse, it looks exactly like them passing through each other.

Worked example

where is the first ace?

A standard 52-card deck is shuffled. What is the expected position of the first ace?

Show solution

The 4 aces cut the 48 other cards into 5 gaps: before the first ace, between consecutive aces, and after the last. By symmetry each non-ace is equally likely to land in any of the 5 gaps. (Think of a random arrangement of 4 aces and one particular non-ace: the non-ace is equally likely to be in any of the 5 relative positions.)

By linearity, the expected number of non-aces before the first ace is 48/5=9.648 / 5 = 9.6, so the expected position of the first ace is

1+485=535=10.6.1 + \frac{48}{5} = \frac{53}{5} = 10.6 .

The direct computation ∑kk(52−k3)/(524)\sum_k k \binom{52-k}{3} / \binom{52}{4} gives the same 53/553/5, with a lot more work. In general, with mm special cards among NN, the first special card is at expected position N+1m+1\frac{N+1}{m+1}.

Invariants: parity and colouring

To show a target state is unreachable, find a quantity that every allowed move preserves and that differs between the start and the target. Common invariants are parity, a sum or difference mod kk, and the count of cells of each colour under a well-chosen colouring.

Mutilated chessboard. Remove two opposite corners from an 8×88 \times 8 board. Can the 62 remaining squares be tiled by 31 dominoes? Opposite corners have the same colour, so 32 squares of one colour and 30 of the other remain. Every domino covers one square of each colour, so 31 dominoes would cover 31 of each. Impossible. (If the two removed squares have different colours, a tiling always exists; the colouring argument only proves impossibility.)

Numbers on a board. Write 1,2,…,101, 2, \dots, 10 on a board. Repeatedly erase two numbers a,ba, b and write ∣a−b∣|a - b|. What can the last number be? Since ∣a−b∣≡a+b(mod2)|a - b| \equiv a + b \pmod 2, the parity of the total never changes. It starts at 5555, odd, so the final number is odd. (In fact every odd value from 1 to 9 can be reached.)

Monovariants: proving a process stops

A monovariant strictly increases (or decreases) with each move and can take only finitely many values, so the process must terminate.

Sign flips. A grid of real numbers; you may flip the sign of every entry in a row or a column. Can you always make every row sum and column sum non-negative? Repeatedly flip any line with a negative sum. Flipping a line whose sum is −s<0-s < 0 raises the total of the whole grid by 2s>02s > 0. There are at most 2rows+columns2^{\text{rows} + \text{columns}} sign patterns, so the total takes finitely many values and cannot rise forever. When the process stops, no line has a negative sum.

The same argument proves you can split any group of people into two rooms so that everyone has at least half their friends in the other room: move anyone who violates it, and the number of friendships inside a room strictly falls.

Worked example

the chameleons

An island has 13 red, 15 green and 17 blue chameleons. When two chameleons of different colours meet, both change to the third colour. Can all 45 chameleons ever be the same colour?

Show solution

Look at how a meeting changes the counts (R,G,B)(R, G, B). A red–green meeting gives (R−1,G−1,B+2)(R - 1, G - 1, B + 2). Every pairwise difference changes by 00 or ±3\pm 3, so each difference mod 3 is invariant.

Initially R−G=−2≡1R - G = -2 \equiv 1, G−B=−2≡1G - B = -2 \equiv 1 and R−B=−4≡2(mod3)R - B = -4 \equiv 2 \pmod 3. All one colour means two counts are 0, so their difference is 0(mod3)0 \pmod 3. No pair starts at 0 mod 30 \bmod 3, so this is impossible.

The invariant also tells you when it is possible. With 13, 16 and 16, G−B=0G - B = 0, and in fact all 45 can become red, green or blue. A breadth-first search over the reachable states confirms both claims. In an interview, find the invariant first, then check that it is the only obstruction for the case asked.

Knowledge check

What is the smallest number of people that guarantees at least three of them were born in the same calendar month?

Knowledge check

The numbers 1 to 10 are on a board. You repeatedly erase two numbers a and b and write a + b if both are even, and |a − b| otherwise. What can you say about the last number?

Knowledge check

100 lockers are closed. Person k toggles every k-th locker, for k = 1, …, 100. How many lockers are open at the end?

Key takeaways

  • Pigeonhole: NN objects in kk boxes force a box with ⌈N/k⌉\lceil N/k \rceil; k(r−1)+1k(r-1) + 1 objects guarantee rr in one box. The work is choosing the boxes: remainders, pairs, odd parts, or labels.
  • Prefix sums plus pigeonhole on remainders finds a consecutive block with sum divisible by nn.
  • Symmetry: mm special items split the rest into m+1m + 1 gaps of equal expected size; indistinguishable objects can be relabelled.
  • Invariants (parity, sums mod kk, colourings) prove impossibility; monovariants over a finite state space prove termination.
  • Find the invariant first, then ask whether it is the only obstruction.

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).