# Program to find decode XORed permutation in Python

Suppose we have an array enc. There is an array perm that is a permutation of the first n(odd) positive integers. This list will be encoded into array enc of length n-1, such that enc[i] = perm[i] XOR perm[i+1]. We have to find the original array perm.

So, if the input is like enc = [2,5,6,3], then the output will be [7, 5, 0, 6, 5], here [7 XOR 5 XOR 0 XOR 6 XOR 5] = [2, 5, 6, 3]

To solve this, we will follow these steps −

• n := size of enc
• result := an array of size (n+1) and fill with 0
• x := 0
• for i in range 1 to n+1, do
• x := x XOR i
• result := x
• for i in range 1 to n, increase by 2, do
• result := result XOR enc[i]
• for i in range 1 to n, do
• result[i] := result[i-1] XOR enc[i-1]
• return result

## Example

Let us see the following implementation to get better understanding −

def solve(enc):
n = len(enc)
result =  * (n+1)
x = 0
for i in range(1, n+2):
x ^= i
result = x
for i in range(1, n+1, 2):
result ^= enc[i]
for i in range(1, n+1):
result[i] = result[i-1] ^ enc[i-1]
return result

enc = [2,5,6,3]
print(solve(enc))

## Input

[2,5,6,3]


## Output

[7, 5, 0, 6, 5]