FastPrepReconstruct an Array from Prefix XOR

Reconstruct an Array from Prefix XOR

NVIDIA logoNVIDIA● EasyINTERNOA

Problem statement

You are given an array of non-negative integers pref. It contains the prefix XORs of an original array arr of the same length.

Using zero-based indexing, pref[i] = arr[0] XOR arr[1] XOR ... XOR arr[i]. Here, XOR is the bitwise exclusive OR operation.

Return the original array arr.

Function

getOriginalArray(pref: int[]) → int[]

Examples

Example 1

pref = [5,2,10]return = [5,7,8]

The prefix XORs of [5,7,8] are 5, 5 XOR 7 = 2, and 5 XOR 7 XOR 8 = 10. They match pref.

Example 2

pref = [2,2,5,6]return = [2,0,7,3]

The prefix XORs of [2,0,7,3] are 2, 2, 5, and 6, in that order.

Example 3

pref = [0]return = [0]

With one element, the prefix XOR is the element itself, so the original array is [0].

Constraints

  • 1 <= pref.length <= 10^5
  • 0 <= pref[i] <= 10^9

More NVIDIA problems

See NVIDIA hiring insights
Saved
public int[] getOriginalArray(int[] pref) {
    // Write your code here.
}
pref[5,2,10]
expected[5,7,8]
Checking account…