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.
You are given an integer array pref of size n. Find and return the array arr of size n that satisfies:
pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i].Note that ^ denotes the bitwise-xor operation.
It can be proven that the answer is unique.
Example 1:
Input: pref = [5,2,0,3,1] Output: [5,7,2,3,2] Explanation: From the array [5,7,2,3,2] we have the following: - pref[0] = 5. - pref[1] = 5 ^ 7 = 2. - pref[2] = 5 ^ 7 ^ 2 = 0. - pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3. - pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
Example 2:
Input: pref = [13] Output: [13] Explanation: We have pref[0] = arr[0] = 13.
Constraints:
1 <= pref.length <= 1050 <= pref[i] <= 106Problem summary: You are given an integer array pref of size n. Find and return the array arr of size n that satisfies: pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]. Note that ^ denotes the bitwise-xor operation. It can be proven that the answer is unique.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[5,2,0,3,1]
[13]
single-number-iii)count-triplets-that-can-form-two-arrays-of-equal-xor)decode-xored-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2433: Find The Original Array of Prefix Xor
class Solution {
public int[] findArray(int[] pref) {
int n = pref.length;
int[] ans = new int[n];
ans[0] = pref[0];
for (int i = 1; i < n; ++i) {
ans[i] = pref[i - 1] ^ pref[i];
}
return ans;
}
}
// Accepted solution for LeetCode #2433: Find The Original Array of Prefix Xor
func findArray(pref []int) []int {
n := len(pref)
ans := []int{pref[0]}
for i := 1; i < n; i++ {
ans = append(ans, pref[i-1]^pref[i])
}
return ans
}
# Accepted solution for LeetCode #2433: Find The Original Array of Prefix Xor
class Solution:
def findArray(self, pref: List[int]) -> List[int]:
return [a ^ b for a, b in pairwise([0] + pref)]
// Accepted solution for LeetCode #2433: Find The Original Array of Prefix Xor
impl Solution {
pub fn find_array(pref: Vec<i32>) -> Vec<i32> {
let n = pref.len();
let mut res = vec![0; n];
res[0] = pref[0];
for i in 1..n {
res[i] = pref[i] ^ pref[i - 1];
}
res
}
}
// Accepted solution for LeetCode #2433: Find The Original Array of Prefix Xor
function findArray(pref: number[]): number[] {
let ans = pref.slice();
for (let i = 1; i < pref.length; i++) {
ans[i] = pref[i - 1] ^ pref[i];
}
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.