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.
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?
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.
[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.
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.
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.
An invariant proves impossibility. To claim "Yes" you also need a recipe. Necessity plus a construction gives the full if-and-only-if.
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.
Try 1 (odd → the parity box stays red, zero is
unreachable) or 2 5 7 (even → Auto-solve collapses it to [0]).
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.
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.
| 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.
// 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; }
O(N) time, O(1) space — optimal, since you must at least read every element to know its parity.
Searching for the actual split/merge sequence explodes combinatorially. Feasibility questions want an invariant, not a plan.
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.
It isn't — merge changes it (26→24). Only its parity is preserved. Track the bit, not the total.
"Odd can't work" doesn't prove "even always works." You need the split-into-ones construction for the other direction.
10⁵ elements near 2³¹ overflow a 32-bit sum and can flip the parity verdict. Use long, or track parity directly.
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.
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.