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 <= 105
0 <= pref[i] <= 106
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are given a series of encoded messages and we want to find the original messages. The brute force approach means we'll try every possible combination of original messages until we find the right one. This involves guessing, checking if the guess is correct, and repeating until we are successful.
Here's how the algorithm would work step-by-step:
def find_original_array_brute_force(encoded_array):
array_length = len(encoded_array)
if array_length == 0:
return []
for first_element_guess in range(201):
original_array_guess = [first_element_guess]
is_valid = True
# Iterate through the encoded array to construct the original array.
for i in range(1, array_length):
next_original_element = encoded_array[i] ^ original_array_guess[i - 1]
original_array_guess.append(next_original_element)
# Verify if the generated array matches the encoded array.
prefix_xor = 0
for i in range(array_length):
prefix_xor ^= original_array_guess[i]
if prefix_xor != encoded_array[i]:
is_valid = False
break
# If the generated array is valid, return it.
if is_valid:
return original_array_guess
return []
The problem gives us a series of values where each value is the cumulative XOR of the original array. To find the original array, we can reverse this process by using the properties of XOR. The key idea is that XORing a number with itself results in zero, and XOR is its own inverse operation.
Here's how the algorithm would work step-by-step:
def find_original_array(prefix_xor_array):
original_array = [0] * len(prefix_xor_array)
# The first element is the same
original_array[0] = prefix_xor_array[0]
for index in range(1, len(prefix_xor_array)):
# XOR with the previous element cancels out
# the cumulative XOR, leaving the single value.
original_array[index] = prefix_xor_array[index] ^ prefix_xor_array[index - 1]
return original_array
Case | How to Handle |
---|---|
Null or empty input array `pref` | Return an empty array if `pref` is null or empty, as there's no original array to derive. |
Input array `pref` with a single element | Return an array containing only the first element of `pref`, as the original array would consist of just that element. |
Integer overflow when calculating XOR | The XOR operation can potentially lead to integer overflow if the values in `pref` are very large; consider using larger data types or modular arithmetic if necessary depending on constraints. |
Large input array `pref` to assess time and space complexity. | Ensure the solution has linear time and space complexity (O(n)), avoiding nested loops or data structures that grow excessively with input size. |
All elements in `pref` are the same. | The original array will contain the first element of `pref` and the rest will be zeros, handled correctly by the iterative XOR difference. |
Zero value in `pref` at any index | The XOR operation handles zeros correctly, so the logic should naturally account for it by producing the prior array value. |
Negative numbers as values in the XOR result (if applicable/allowed in the language) | Check the programming language's handling of negative numbers in XOR operation and ensure correct output based on language specification. |
Pref array is monotonically increasing or decreasing | Prefix XOR doesn't mandate increasing or decreasing properties, the iterative XOR-difference approach will still reconstruct the array correctly. |