Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find decode XORed permutation in Python
Suppose we have an array enc that represents an encoded permutation. There is an array perm that is a permutation of the first n (odd) positive integers. This list is encoded into array enc of length n-1, such that enc[i] = perm[i] XOR perm[i+1]. We need 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, 5 XOR 0, 0 XOR 6, 6 XOR 5] = [2, 5, 6, 3].
Algorithm
To solve this, we will follow these steps ?
- Calculate
nas the size ofenc - Create a
resultarray of size (n+1) and fill with 0 - Calculate XOR of all numbers from 1 to n+1
- Find the first element by XORing with alternate elements of
enc - Generate remaining elements using the XOR relationship
- Return the result
How It Works
The key insight is that if we XOR all elements of the original permutation, we get the XOR of numbers 1 to n+1. Since enc[i] = perm[i] XOR perm[i+1], we can use this property to find the first element, then derive the rest.
Example
Let us see the following implementation to get better understanding ?
def solve(enc):
n = len(enc)
result = [0] * (n + 1)
# Calculate XOR of all numbers from 1 to n+1
x = 0
for i in range(1, n + 2):
x ^= i
# Find the first element
result[0] = x
for i in range(1, n + 1, 2):
result[0] ^= enc[i]
# Generate remaining elements
for i in range(1, n + 1):
result[i] = result[i-1] ^ enc[i-1]
return result
# Test the function
enc = [2, 5, 6, 3]
perm = solve(enc)
print("Original permutation:", perm)
# Verify the result
print("Verification:")
for i in range(len(enc)):
print(f"perm[{i}] XOR perm[{i+1}] = {perm[i]} XOR {perm[i+1]} = {perm[i] ^ perm[i+1]}")
Original permutation: [7, 5, 0, 6, 5] Verification: perm[0] XOR perm[1] = 7 XOR 5 = 2 perm[1] XOR perm[2] = 5 XOR 0 = 5 perm[2] XOR perm[3] = 0 XOR 6 = 6 perm[3] XOR perm[4] = 6 XOR 5 = 3
Step-by-Step Breakdown
def solve_with_steps(enc):
print(f"Input encoded array: {enc}")
n = len(enc)
print(f"Length n = {n}")
# Step 1: Calculate XOR of 1 to n+1
x = 0
for i in range(1, n + 2):
x ^= i
print(f"XOR of numbers 1 to {n+1}: {x}")
# Step 2: Find first element
result = [0] * (n + 1)
result[0] = x
print(f"Initial result[0] = {x}")
for i in range(1, n + 1, 2):
result[0] ^= enc[i]
print(f"After XOR with enc[{i}] = {enc[i]}: result[0] = {result[0]}")
# Step 3: Generate remaining elements
for i in range(1, n + 1):
result[i] = result[i-1] ^ enc[i-1]
print(f"result[{i}] = result[{i-1}] ^ enc[{i-1}] = {result[i-1]} ^ {enc[i-1]} = {result[i]}")
return result
# Example with step-by-step output
enc = [2, 5, 6, 3]
result = solve_with_steps(enc)
print(f"\nFinal result: {result}")
Input encoded array: [2, 5, 6, 3] Length n = 4 XOR of numbers 1 to 5: 1 Initial result[0] = 1 After XOR with enc[1] = 5: result[0] = 4 After XOR with enc[3] = 3: result[0] = 7 result[1] = result[0] ^ enc[0] = 7 ^ 2 = 5 result[2] = result[1] ^ enc[1] = 5 ^ 5 = 0 result[3] = result[2] ^ enc[2] = 0 ^ 6 = 6 result[4] = result[3] ^ enc[3] = 6 ^ 3 = 5 Final result: [7, 5, 0, 6, 5]
Conclusion
This algorithm efficiently decodes a XORed permutation by leveraging the mathematical properties of XOR operations. The key insight is using the XOR of all numbers 1 to n+1 and then applying the encoding relationship in reverse to reconstruct the original permutation.
