1338. Decode XOR Encoded Array

MediumBit ManipulationBit ManipulationXORArray

An array of n+1 non-negative integers was encoded into an array of length n where encoded[i] equals original[i] XOR original[i+1]. Given the encoded array and the first original element, reconstruct and return the original array.

Input: A JSON object {"encoded": [<integers>], "first": <integer>}.

Output: Return the reconstructed original array of length len(encoded) + 1.

Examples

Example 1
Input: {"encoded":[1,2,3],"first":1}
Output: [1,0,2,1]
Explanation: Chaining XORs gives [1,0,2,1].
Example 2
Input: {"encoded":[6,2,7,3],"first":4}
Output: [4,2,0,7,4]
Explanation: Rebuilding step by step from 4.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →