![]() |
VOOZH | about |
Given an array arr[] of size N, find the Prefix Xor of the array. A prefix xor array is another array prefixXor[] of the same size, such that the value of prefixXor[i] is arr[0] ^ arr[1] ^ arr[2] . . . arr[i].
Examples:
Input: arr[] = {10, 20, 10, 5, 15}
Output:prefixXor[] = {10, 30, 20, 17, 30 }
Explanation: While traversing the array, update the element by xoring it with its previous element.
prefixXor[0] = 10,
prefixXor[1] = prefixXor[0] ^ arr[1] = 30,
prefixXor[2] = prefixXor[1] ^ arr[2] = 20 and so on.Input: arr[]={1,2,1,2,5}
Output: prefixXor[] = {1, 3, 2, 0, 5 }
Approach: To solve the problem follow the given steps:
Below is the implementation of the approach:
Given Array: 10 20 10 5 15 Prefix Xor: 10 30 20 17 30
Time Complexity: O(N), where N is the size of input array
Auxiliary Space: O(N)
Problem | Practice Link |
|---|---|
XOR of a given range | |
XOR of all elements | |
Game of xor | |