You are given an encoded array where encoded[i] = arr[i] XOR arr[i + 1] and the first element of arr is also given. Reconstruct the original array. Pattern focus: XOR State Tracking. Recover each next value by XORing the current prefix state with the encoded value.
encoded = XOR differences, first = first element of the original array
the reconstructed original array
Example 1:
Input:
encoded = [1,2,3] first = 1
Output:
[1,0,2,1]
Explanation:
Each next value is obtained by XORing the current value with the next encoded entry.
Example 2:
Input:
encoded = [6,2,7,3] first = 4
Output:
[4,2,0,7,4]
Explanation:
The XOR chain is followed step by step.