Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There is an integer array perm that is a permutation of the first n positive integers, where n is always odd.
It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1].
Given the encoded array, return the original array perm. It is guaranteed that the answer exists and is 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]
Constraints:
3 <= n < 105n is odd.encoded.length == n - 1Problem summary: There is an integer array perm that is a permutation of the first n positive integers, where n is always odd. It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1]. Given the encoded array, return the original array perm. It is guaranteed that the answer exists and is unique.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[3,1]
[6,5,4,6]
find-xor-beauty-of-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1734: Decode XORed Permutation
class Solution {
public int[] decode(int[] encoded) {
int n = encoded.length + 1;
int a = 0, b = 0;
for (int i = 0; i < n - 1; i += 2) {
a ^= encoded[i];
}
for (int i = 1; i <= n; ++i) {
b ^= i;
}
int[] perm = new int[n];
perm[n - 1] = a ^ b;
for (int i = n - 2; i >= 0; --i) {
perm[i] = encoded[i] ^ perm[i + 1];
}
return perm;
}
}
// Accepted solution for LeetCode #1734: Decode XORed Permutation
func decode(encoded []int) []int {
n := len(encoded) + 1
a, b := 0, 0
for i := 0; i < n-1; i += 2 {
a ^= encoded[i]
}
for i := 1; i <= n; i++ {
b ^= i
}
perm := make([]int, n)
perm[n-1] = a ^ b
for i := n - 2; i >= 0; i-- {
perm[i] = encoded[i] ^ perm[i+1]
}
return perm
}
# Accepted solution for LeetCode #1734: Decode XORed Permutation
class Solution:
def decode(self, encoded: List[int]) -> List[int]:
n = len(encoded) + 1
a = b = 0
for i in range(0, n - 1, 2):
a ^= encoded[i]
for i in range(1, n + 1):
b ^= i
perm = [0] * n
perm[-1] = a ^ b
for i in range(n - 2, -1, -1):
perm[i] = encoded[i] ^ perm[i + 1]
return perm
// Accepted solution for LeetCode #1734: Decode XORed Permutation
struct Solution;
impl Solution {
fn decode(encoded: Vec<i32>) -> Vec<i32> {
let n = encoded.len() + 1;
let mut first = 0;
for i in 0..n {
first ^= (i + 1) as i32;
}
for i in 0..n - 1 {
if i % 2 == 1 {
first ^= encoded[i];
}
}
let mut res = vec![first];
for i in 1..n {
res.push(res[i - 1] ^ encoded[i - 1]);
}
res
}
}
#[test]
fn test() {
let encoded = vec![3, 1];
let res = vec![1, 2, 3];
assert_eq!(Solution::decode(encoded), res);
let encoded = vec![6, 5, 4, 6];
let res = vec![2, 4, 1, 5, 3];
assert_eq!(Solution::decode(encoded), res);
}
// Accepted solution for LeetCode #1734: Decode XORed Permutation
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1734: Decode XORed Permutation
// class Solution {
// public int[] decode(int[] encoded) {
// int n = encoded.length + 1;
// int a = 0, b = 0;
// for (int i = 0; i < n - 1; i += 2) {
// a ^= encoded[i];
// }
// for (int i = 1; i <= n; ++i) {
// b ^= i;
// }
// int[] perm = new int[n];
// perm[n - 1] = a ^ b;
// for (int i = n - 2; i >= 0; --i) {
// perm[i] = encoded[i] ^ perm[i + 1];
// }
// return perm;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.