#1720

Decode XORed Array

newbie · 110 · lc easy +12 · verified · 87.3% accepted · 1,669 likes · top 98%

Description

An 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] == first), reconstruct and return arr. The answer is unique.

Example 1:

Input: encoded = [1,2,3], first = 1
Output: [1,0,2,1]
Explanation: If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]

Example 2:

Input: encoded = [6,2,7,3], first = 4
Output: [4,2,0,7,4]

Code

1
2
3