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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
There is a hidden integer array arr that consists of n non-negative integers.
It was encoded into another integer array encoded of length n - 1, such that encoded[i] = arr[i] XOR arr[i + 1]. For example, if arr = [1,0,2,1], then encoded = [1,2,3].
You are given the encoded array. You are also given an integer first, that is the first element of arr, i.e. arr[0].
Return the original array arr. It can be proved that the answer exists and is unique.
Example 1:
Input: encoded = [1,2,3], first = 1 Output: [1,0,2,1] Explanation: If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]
Example 2:
Input: encoded = [6,2,7,3], first = 4 Output: [4,2,0,7,4]
Constraints:
2 <= n <= 104encoded.length == n - 10 <= encoded[i] <= 1050 <= first <= 105Problem summary: There is a hidden integer array arr that consists of n non-negative integers. It was encoded into another integer array encoded of length n - 1, such that encoded[i] = arr[i] XOR arr[i + 1]. For example, if arr = [1,0,2,1], then encoded = [1,2,3]. You are given the encoded array. You are also given an integer first, that is the first element of arr, i.e. arr[0]. Return the original array arr. It can be proved 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
[1,2,3] 1
[6,2,7,3] 4
find-the-original-array-of-prefix-xor)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1720: Decode XORed Array
class Solution {
public int[] decode(int[] encoded, int first) {
int n = encoded.length;
int[] ans = new int[n + 1];
ans[0] = first;
for (int i = 0; i < n; ++i) {
ans[i + 1] = ans[i] ^ encoded[i];
}
return ans;
}
}
// Accepted solution for LeetCode #1720: Decode XORed Array
func decode(encoded []int, first int) []int {
ans := []int{first}
for i, x := range encoded {
ans = append(ans, ans[i]^x)
}
return ans
}
# Accepted solution for LeetCode #1720: Decode XORed Array
class Solution:
def decode(self, encoded: List[int], first: int) -> List[int]:
ans = [first]
for x in encoded:
ans.append(ans[-1] ^ x)
return ans
// Accepted solution for LeetCode #1720: Decode XORed Array
struct Solution;
impl Solution {
fn decode(encoded: Vec<i32>, first: i32) -> Vec<i32> {
let n = encoded.len();
let mut res = vec![first];
for i in 0..n {
res.push(res[i] ^ encoded[i]);
}
res
}
}
#[test]
fn test() {
let encoded = vec![1, 2, 3];
let first = 1;
let res = vec![1, 0, 2, 1];
assert_eq!(Solution::decode(encoded, first), res);
let encoded = vec![6, 2, 7, 3];
let first = 4;
let res = vec![4, 2, 0, 7, 4];
assert_eq!(Solution::decode(encoded, first), res);
}
// Accepted solution for LeetCode #1720: Decode XORed Array
function decode(encoded: number[], first: number): number[] {
const ans: number[] = [first];
for (const x of encoded) {
ans.push(ans.at(-1)! ^ x);
}
return ans;
}
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.