The three arguments behind most ad hoc and constructive problems:

  • An invariant proves something is impossible.
  • A monovariant proves a process terminates and bounds the number of moves.
  • An extremal object gives the first step of a proof or construction.

Invariants

An invariant is a quantity that no allowed move changes. If start and target differ in it, the target is unreachable.

MovePreserved
swap two elementspermutation parity flips, so it’s preserved under pairs of swaps
replace by total sum, gcd of all
replace by gcd of all, parity of the sum
add to two elementspairwise differences mod
flip a cell and its neighboursXOR of the cells in a fixed “test set” orthogonal to every move (linear algebra over )
reverse a subarray of length multiset; parity of the permutation changes by
adjacent swap of different valuesinversion count changes by exactly
slide a tile in the 15-puzzlepermutation parity + row of the blank, mod 2
rotate a cube facecorner orientation sum mod 3, edge orientation mod 2, permutation parities equal

Candidates to try in order: sum, sum mod (try first, then 3), XOR, gcd, the multiset or sorted order, inversions, a colouring count, a weighted sum with weights chosen so every move preserves it.

Linear invariants over . When every move adds (XORs) a fixed vector to the state (lights out, toggling a row/column, flipping a path), the reachable set is the start plus the span of the move vectors. Reachability is a linear system, and the invariants are the vectors orthogonal to all moves. Solve with Gaussian elimination or an XOR basis.

Necessary and sufficient. An invariant only proves impossibility. The standard solution shape is:

  1. compute the invariant and output “impossible” if it differs;
  2. otherwise construct a solution, usually by induction (fix one element, recurse on the rest).

Often the answer is that the invariant is the only obstruction. Brute-force tiny cases to check that before proving it — this step is not optional: a brute force over answers “which cases are possible” in seconds and makes the invariant visible, instead of guessing at a pattern from nothing.

Monovariants and termination

A monovariant only increases or only decreases. A non-negative integer that strictly decreases at each step proves termination, and its initial value bounds the number of steps.

ProcessMonovariant
bubble sort / adjacent swapsinversions: exactly swaps are necessary and sufficient
Euclid’s algorithm; at least halves every two steps
local improvement of a cut or a matchingobjective value (bounded integer)
augmenting pathsflow value
”if two neighbours are out of order, fix them” of squares, or the lexicographic order of the sorted state
chip-firinga potential such as
Stable matching (Gale-Shapley)each proposer moves down their list

A real number decreasing toward 0 need not terminate; you need a well-founded measure (bounded integers, or lexicographic tuples of them).

Bounding the answer: “minimum number of operations” problems are often: show each operation decreases potential by at most , so the answer is at least , then construct a sequence achieving it.

Parity and colouring

FactUse
, so the number of odd-degree vertices is even (in any subgraph too)Euler paths, Chinese postman, Christofides, -joins; Sperner’s lemma and “handshake on a hypergraph” generalise this and give a combinatorial proof of Brouwer’s fixed-point theorem
bipartite no odd cycle2-colour by BFS
a permutation with cycles on elements has sign sorting by swaps needs swaps
odd bitwiseLucas mod 2, Sierpinski patterns
parity of sums
a closed walk in a bipartite graph has even lengthgrid walks: flips every step
knight moves change square colourclosed knight tours need an even number of squares

Colouring arguments. Colour the board so every piece covers a fixed count of each colour:

  • Chessboard with opposite corners removed has two more of one colour, so no domino tiling.
  • bars on an board: colour cell with . Each bar covers one of each colour, so every colour count must be equal.
  • four-colouring for -ish pieces and L-trominoes.
  • Conway’s soldiers: weight a cell at Manhattan distance from the target by with (so ). Every jump keeps the total weight from increasing, and the infinite half-plane has total weight exactly 1 relative to row 5, so row 5 is unreachable.

The rule: when stuck on “is it possible”, tabulate small cases and look at them mod 2, then mod 3, then under a colouring, then as a weighted sum.

Try mod 2 first, always

It is the cheapest hypothesis and correct more often than any other single guess. If mod 2 fails, try mod 3, then a colouring, then a weighted sum. A potential function (strictly monotone rather than invariant) is the fallback once no fixed quantity works — that’s a monovariant, not an invariant.

More parity facts, and where they decide an answer outright rather than just narrowing the search:

Fact
, parity has period 4 in odd for , even for
Eulerian circuit iff every degree even; Eulerian path iff exactly two oddEuler Tours
Chinese postman: pair up the (even number of) odd-degree vertices by shortest paths and duplicate those pathsmakes every degree even, so an Eulerian circuit exists on the duplicated multigraph
misère Nim’s deviation from normal Nim depends on the parity of the number of heaps of size 1Nim
”take 1 or 2 stones” has Grundy value parity generalised to mod 3
-trominoes tile a board with any one cell removedclassic induction: quarter the board, the removed cell’s quadrant needs one more tromino at the centre, which also covers one cell in each of the other three quadrants, recursing
triangular or toroidal boardsthe same colouring idea re-indexes: 3-colour a triangular grid by ; a toroidal colouring just wraps the indices mod the board’s period

The extremal principle

Consider the largest, smallest, first, last, or otherwise extreme object. It has properties nothing else has.

Minimal counterexample: assume the claim fails, take the smallest counterexample, and build a smaller one. This is strong induction in disguise.

Extremal element of an optimal solution: take an optimal solution and look at its extreme part (heaviest edge, last job, leftmost point). Show it can be exchanged or removed without loss. That’s the core of exchange arguments.

ClaimExtremal object
every tree with vertices has leavesendpoints of a longest path
min degree implies a cyclelongest path: its endpoint’s other neighbour lies on the path
min degree implies a path of length and a cycle of length longest path again
every DAG has a sourcestart of a longest path
every tournament has a Hamiltonian pathinsert vertices one by one, or take a longest path
every tournament has a king (reaches all in steps)vertex of maximum out-degree
the heaviest edge of a cycle isn’t in some MSTcycle property via exchange
Sylvester-Gallaismallest positive distance from a point to a line through two others
irrationalminimal denominator

Where to look:

SettingExtreme
graphslongest path, max/min degree, heaviest edge, largest component, deepest DFS vertex
treesleaves, centroid, diameter endpoints, deepest node
sequencesmaximum (split the array at it), first violation, longest run
geometryleftmost/lowest point (on the hull), closest pair, extreme point in a direction
constructionsmost constrained element first; the largest value placed first

Splitting at the maximum is a great general tool: many problems on arrays decompose recursively around the position of the maximum (Cartesian tree, “divide at the max” with small-to-large to get ).

A linear function on an interval is extremised at an endpoint, and a convex function on a polytope at a vertex. Only test the boundary values.

Pigeonhole

items in boxes force some box to hold at least .

Application
some non-empty subarray has sum divisible by prefix sums mod ; find two equal in
among numbers in two are coprime, and one divides anotherconsecutive pairs; odd parts
iterating on a set of size repeats within stepscycle detection, period finding
a string of length repeats a letteranswer bounded by alphabet size
bounded answer means brute force”if the answer is always yes”, so only search when
Erdős-Szekeres: distinct numbers contain an increasing run of length or a decreasing one of length LIS/LDS bounds;
distinct prefix gcds/ORs/ANDs ending at a positionat most
birthday bound: random samples from collide with probability hashing collision estimates

Pigeonhole proves existence. You still have to scan for the pair.

Dilworth and Mirsky. In a partial order, the size of the largest antichain equals the minimum number of chains covering it (Dilworth), and the length of the longest chain equals the minimum number of antichains (Mirsky). For sequences: the minimum number of non-increasing subsequences covering equals the length of the longest strictly increasing subsequence. Computing a minimum chain cover in a general DAG is a bipartite matching (path cover).