- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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[0] := x
- for i in range 1 to n, increase by 2, do
- result[0] := result[0] 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 = [0] * (n+1) x = 0 for i in range(1, n+2): x ^= i result[0] = x for i in range(1, n+1, 2): result[0] ^= 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]

- Related Articles
- Program to recover decode XORed array in Python
- Program to find maximum sum obtained of any permutation in Python
- C++ program to find maximum possible value of XORed sum
- Program to find number of ways we can decode a message in Python
- C++ Program to find permutation from merged permutations
- C++ program to find permutation with n peaks
- C++ program to find maximum possible value for which XORed sum is maximum
- Program to find number of elements in all permutation which are following given conditions in Python
- Decode Ways in Python
- Python Program for BogoSort or Permutation Sort
- Program to find out the sum of numbers where the correct permutation can occur in python
- Java Program to decode string to integer
- Program to decode a given message in C++
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- C++ program to find number of l-r pairs for which XORed results same as summation

Advertisements