A hidden integer array arr of n non-negative integers was encoded into encoded of length n - 1, where encoded[i] = arr[i] XOR arr[i + 1]. Given encoded and the first element first = arr[0], return the original array arr. The answer exists and is unique.
A hidden integer array arr of n non-negative integers was encoded into encoded of length n - 1, where encoded[i] = arr[i] XOR arr[i + 1]. Given encoded and the first element first = arr[0], return the original array arr. The answer exists and is unique.
Example 1:
Input: {"encoded":[1,2,3],"first":1}
Output: [1,0,2,1]
Constraints:
2≤n≤104
encoded.length=n−1
0≤encoded[i]≤105
0≤first≤105
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"encoded":[1,2,3],"first":1}
Each encoded[i] is the XOR of two neighbours, arr[i] ^ arr[i + 1]. Since x ^ x = 0, XORing a known neighbour back in reveals the other one.