aboutsummaryrefslogtreecommitdiff
path: root/find_orig_prefix_xor.py
blob: d92df0ee28231b0bfc3d2ab916334826ee835596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 2433. Find The Original Array of Prefix Xor

"""
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 operator. it can be proven that the answer is
unique
"""

class Solution(object):
    def findArray(self, pref):
        for i in range(len(pref) - 1, 0, -1):
            pref[i] ^= pref[i - 1]
        return pref


if __name__ == "__main__":
    obj = Solution()
    print(obj.findArray([5,2,0,3,1])) # expect: 5 7 2 3 2
    print(obj.findArray([13])) # expect: 13