LC 89

Gray Code

Medium · LC 1–100

backtrackingbit-manipulationmath
Free

Candidate solution routes

Primary

Constraint-satisfaction backtracking

Free

Invariant: At recursion entry, the partial assignment satisfies every activated constraint and every auxiliary set/count exactly matches it; a pruned branch has no legal completion.

candidate confidence: 0.473 · metadata_only

Alternate 1

Bitwise invariants / XOR cancellation

Free

Invariant: The running XOR equals the XOR of values with odd processed frequency; per-bit counts can be reduced independently modulo a repetition count.

candidate confidence: 0.610 · metadata_only

Alternate 2

Subset / combination backtracking

Free

Invariant: On entering dfs(start), path indices are strictly increasing and already fixed below start. The call generates exactly completions whose next index is at least start, preventing duplicate orders.

candidate confidence: 0.470 · metadata_only

Alternate 3

Permutation backtracking

Free

Invariant: At depth pos, path contains exactly pos distinct indices, used matches path exactly, and equal values at one level expand through only their canonical representative.

candidate confidence: 0.440 · metadata_only

How to use this page

Read the statement and constraints first, then validate recognition signals, invariant, and complexity. Candidate score is not correctness evidence.