
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to recover decode XORed array in Python
Suppose we have a hidden array arr with n non-negative integers. Now this array is encoded into another array enc of length n-1. So here enc[i] = arr[i] XOR arr[i+1]. If we have the encoded enc array and an integer first, that is the first element of actual array, we have to find the original array.
So, if the input is like enc = [8,3,2,7], first = 4, then the output will be [4, 12, 15, 13, 10].
To solve this, we will follow these steps −
arr := an array with only one element first
for i in range 0 to size of enc - 1, do
insert arr[i] XOR enc[i] at the end of arr
return arr
Example (Python)
Let us see the following implementation to get better understanding −
def solve(enc, first): arr = [first] for i in range(0, len(enc)): arr.append(arr[i] ^ enc[i]) return arr enc = [8,3,2,7] first = 4 print(solve(enc, first))
Input
[8,3,2,7], 4
Output
[4, 12, 15, 13, 10]
- Related Articles
- Program to find decode XORed permutation in Python
- Program to recover shuffled queue of people in python
- Decode Ways in Python
- Program to find number of ways we can decode a message in Python
- C++ program to find maximum possible value of XORed sum
- Java Program to decode string to integer
- Program to decode a given message in C++
- Program to decode a run-length form of string into normal form in Python
- To decode string array values that is already encoded in Numpy
- C++ program to find maximum possible value for which XORed sum is maximum
- How to recover YouTube Channel?
- C++ Program to Decode a Message Encoded Using Playfair Cipher
- Encode and decode uuencode files using Python
- What is the difference between encode/decode in Python?
- Program to minimize deviation in array in Python

Advertisements