A chain of n relay beacons transmits signal codes down a line. Each beacon i (numbered 0 to n-1) broadcasts a running checksum equal to the XOR of its own signal code together with every signal code transmitted by the beacons before it — that is, if code[i] denotes beacon i's original signal code, the broadcast checksum is checksum[i] = code[0] XOR code[1] XOR ... XOR code[i].
A recovered log contains only the broadcast checksums, in order. Recover the original signal code for every beacon.
n — the number of beacons.n integers checksum[0], checksum[1], ..., checksum[n-1] — the recorded running checksums.Print n integers separated by single spaces on one line: the recovered signal codes code[0], code[1], ..., code[n-1], in beacon order.
Example 1
Input
4 5 2 0 3
Expected
5 7 2 3
Explanation
The checksum sequence is 5, 2, 0, 3. Beacon 0's code equals its own checksum: 5. Beacon 1's code is checksum[1] XOR checksum[0] = 2 XOR 5 = 7. Beacon 2's code is checksum[2] XOR checksum[1] = 0 XOR 2 = 2. Beacon 3's code is checksum[3] XOR checksum[2] = 3 XOR 0 = 3. Re-accumulating the recovered codes by XOR in order reproduces 5, then 5 XOR 7 = 2, then 2 XOR 2 = 0, then 0 XOR 3 = 3, matching the input exactly. The output is `5 7 2 3`.
Example 2
Input
1 13
Expected
13
Explanation
With only one beacon, its code equals its own checksum directly, since there are no earlier beacons to XOR with: 13. The output is `13`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →