Data Structures · Invariants · A Field Guide

Reaching zero by
split and merge

Split a number into two that sum to it; merge two into their XOR. Can you collapse the whole array to a single zero? Don't hunt for the moves — find the property the moves can't touch.

sum is even → Yes · odd → No
01 · Strip the story away

Two operations, one target: [0]

Two moves are allowed, repeatable in any order. Split: replace B with any C, D where C + D = B. Merge: replace P, Q with P XOR Q. Goal: reduce the array to a single [0]. The tempting move is to simulate — which to merge, what to split into, in what order. There are far too many sequences. The right question is different: what property survives every operation?


02 · Chase what can't change

The sum survives split; its parity survives both

A split leaves the total sum untouched: C + D = B. A merge does change the sum — 9+17=26 becomes 9 XOR 17 = 24 — so the sum itself isn't invariant. But watch the last bit. There's an identity:

P + Q = (P XOR Q) + 2·(P AND Q)

The gap P + Q − (P XOR Q) = 2·(P AND Q) is always even, so P + Q and P XOR Q always share parity. Split can't change the sum; merge can't change its parity. Therefore sum mod 2 is invariant.

The verdict [0] has sum 0 — even. So an odd starting sum can never reach zero. Even sum is necessary. The constructive half (below) shows it's also sufficient: possible ⇔ sum is even.

A locked odometer digit

You can spin the wheels however you like, but one digit is glued in place. Parity is that digit — every split and merge leaves it exactly where it started.

Pairing off dancers

Split everything down to 1s. Two odd partners cancel: 1 XOR 1 = 0. An even crowd pairs off completely; one left over can never find a partner.

Two proofs, not one

An invariant proves impossibility. To claim "Yes" you also need a recipe. Necessity plus a construction gives the full if-and-only-if.


04 · Try to break the invariant

Split, merge, watch parity refuse to move

Tap a token to select it. Split the selected one into two parts (enter the first part); Merge two selected tokens into their XOR. Odd values carry a red underline, even ones teal. The parity box up top never changes color no matter what you do — that's the invariant, live. Reach a single [0] when the sum is even.

Split & merge sandbox

current sum
–
sum mod 2 (invariant)
–
# tokens
–
Select one token to split, or two to merge.

Try 1 (odd → the parity box stays red, zero is unreachable) or 2 5 7 (even → Auto-solve collapses it to [0]).


05 · Why even is enough

The recipe: shatter into ones, then pair

The invariant proves odd is hopeless. Sufficiency needs a construction, and it's simple. Split every value all the way down to 1s — a number v becomes v ones. The array turns into S ones total, where S is the sum.

Even sum → even # of ones
1 XOR 1 = 0
Pair them all off into zeros; nothing is left unpaired.
Collapse the zeros
0 XOR 0 = 0
Merge zeros repeatedly until a single [0] remains.

So every even-sum array reaches zero, and no odd-sum array can. That closes both directions: possible if and only if the sum is even.


06 · Trace table

Reading the answer off the parity

Input sum parity # odd elements Answer
[9, 17] 26 even 2 Yes
[1] 1 odd 1 No
[2, 5, 7] 14 even 2 Yes
[3, 4, 8] 15 odd 1 No

The sum is even exactly when the count of odd elements is even — the two tests are equivalent, since only odd terms flip the last bit.


07 · The code

One bit is all that matters

// Clearest: sum in long (avoids int overflow), check the low bit
boolean canMakeZero(int[] arr) {
    long sum = 0;
    for (int v : arr) sum += v;
    return sum % 2 == 0;
}

// Deeper: track only parity — no overflow possible at all
boolean canMakeZero(int[] arr) {
    int parity = 0;
    for (int v : arr) parity ^= (v & 1);
    return parity == 0;
}
Why long for the sum
10⁵ × ~2³¹ ≫ INT_MAX
The full sum overflows int; long holds it. Or sidestep it entirely with parity.
Why parity needs no sum
parity ^= (v & 1)
Only the last bit decides the answer, so accumulate that one bit and nothing overflows.

O(N) time, O(1) space — optimal, since you must at least read every element to know its parity.


08 · Traps to avoid

Five ways it slips

Simulating the operations

Searching for the actual split/merge sequence explodes combinatorially. Feasibility questions want an invariant, not a plan.

Guessing whole-array XOR

Because a merge uses XOR, it's tempting to test XOR(arr)==0. But 9^17=24≠0 while the answer is Yes. Splitting destroys any XOR invariant.

Thinking the sum is invariant

It isn't — merge changes it (26→24). Only its parity is preserved. Track the bit, not the total.

Proving only necessity

"Odd can't work" doesn't prove "even always works." You need the split-into-ones construction for the other direction.

int sum overflow

10⁵ elements near 2³¹ overflow a 32-bit sum and can flip the parity verdict. Use long, or track parity directly.


09 · The pattern

Arbitrary operations + "is it reachable?" → find an invariant

This breaks cleanly from the binary-search problems: there's no search space to halve. When a problem allows many arbitrary transformations and asks only whether a target is reachable, the move is to ask what every operation preserves — parity, a sum, a GCD, a count, something mod m. If an operation only ever changes a quantity by multiples of m, that quantity mod m is invariant.

Necessity
invariant differs → impossible
the target's invariant must match the start's
Sufficiency
construct a witness sequence
show a concrete path when the invariant matches

The wording is the tell: "sorted + O(log n)" says binary search; "arbitrary allowed operations + feasibility" says invariant. And a complete answer pairs the impossibility proof with a construction to earn the if-and-only-if.


10 · One month from now

Reconstruct it, don't memorize it

  1. Feasibility under arbitrary operations → stop simulating, look for an invariant.
  2. Split keeps the sum (C+D=B); merge changes it but keeps its parity (P+Q and P XOR Q agree mod 2).
  3. So sum mod 2 never changes; [0] is even, so an odd sum is impossible.
  4. Even sum is enough: split everything into 1s, pair them (1 XOR 1 = 0), merge the zeros.
  5. Answer: even sum → Yes, odd sum → No. Track just the low bit; O(N), O(1).
The transferable insight When many arbitrary transformations are allowed and you only need to know if a state is reachable, search for an invariant before simulating — and don't stop at necessity. Pair the invariant with a construction to prove reachability both ways.