Decode XORed Array
In this lab, you will be working with a hidden integer array arr
consisting of n
non-negative integers. This array has been encoded into another integer array named encoded
with a length of n - 1
, such that encoded[i] = arr[i] XOR arr[i + 1]
. For instance, if arr = [1,0,2,1]
, then encoded = [1,2,3]
.
Your objective is to return the original array arr
based on the given encoded
array. You will also be provided an integer first
, which is the first element of arr
, i.e., arr[0]
. It can be proven that the answer exists and is unique.
Examples
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]
Constraints
2 <= n <= 104
encoded.length == n - 1
0 <= encoded[i] <= 105
0 <= first <= 105
Challenges
- Implement the
decode
function that takes theencoded
array and thefirst
integer as inputs and returns the original arrayarr
. - Export the
decode
function so it can be imported in the evaluation script. - Make sure your code is free of syntax and logical errors.
For the challenges above, you need to export the required variables and functions so that they can be tested in the evaluation script.
Good luck!