#1734

Decode XORed Permutation

specialist · 605 · lc medium +29 · verified · 66.9% accepted · 802 likes · top 73%

Description

An integer array perm is a permutation of the first n positive integers (n is always odd), encoded as encoded[i] = perm[i] XOR perm[i + 1].

Given encoded, recover and return perm. The answer is guaranteed unique.

Example 1:

Input: encoded = [3,1]
Output: [1,2,3]
Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]

Example 2:

Input: encoded = [6,5,4,6]
Output: [2,4,1,5,3]

Code

1
2
3