Ctrl K

Undo the Neighbor XOR

Easy
There was an array arr of n non-negative integers, but only two things about it were kept: - an array links of length n - 1, where links[i] = arr[i] XOR arr[i + 1]; - the first element, start = arr[0]. Rebuild and return the original array arr. It can be shown that it is determined uniquely.
Example 1
links=[5, 5, 0], start=3 → [3, 6, 3, 3]

3 XOR 5 = 6, 6 XOR 5 = 3, 3 XOR 0 = 3. Check: 3^6 = 5, 6^3 = 5, 3^3 = 0.

Example 2
links=[8], start=0 → [0, 8]

With 0 as the first element, the second is 0 XOR 8 = 8.

Constraints
  • 2 ≤ n ≤ 10⁴, len(links) = n - 1
  • 0 ≤ links[i] ≤ 10⁵
  • 0 ≤ start ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Unpaired Value EasyThe Lost Ticket EasyDoubled Tag, Lost Tag EasyMirror and Negate a Bitmap EasyMultiple of Five at Every Step Easy
loading Python…
Input
links=[5, 5, 0], start=3
Expected
[3, 6, 3, 3]

Run checks the first 3 tests in your browser. Submit checks all 96 tests on the server, hidden ones included.