[LeetCode] Find The Original Array of Prefix Xor

2433. Find The Original Array of Prefix Xor

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<int> findArray(vector<int>& pref) {
for(int i = 0, now = 0; i < pref.size(); i++) {
int n = pref[i] ^ now;
pref[i] = n;
now ^= pref[i];
}
return pref;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/09/PS/LeetCode/find-the-original-array-of-prefix-xor/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.