# C++ Queries on XOR of XORs of All Subarrays

C++Server Side ProgrammingProgramming

To calculate the XOR of all the subarrays present in the given range and print it. For example

Input : arr[] = { 4, 1, 2, 3, 5 }, Q = 3

Queries

q1 = { 1, 2 }
q2 = { 2, 4 }
q3 = { 1, 4 }

Output : 0
2
0
Explanation : As the given problem states that we need to find XOR of all the subarrays present in the given range so as for query 2 the subarrays are :
{1}, {2}, {3}, {1, 2}, {2, 3}, (1, 2, 3}
So as you can see the number of occurrences of elements are :
1 is present 3 times
2 is present 4 times
3 is present 3 times
Now as we know the property of XOR is that the numbers that are present an even number of times get canceled out so out 2 got canceled out and just the XOR of 3 and 1 remained and that was our answer.

We need to observe the pattern being formed in this problem, and then we need to implement it accordingly.

## Approach to Find the Solution

In this problem, we are trying to find the in-between patterns present in the problem. When. When we see that pattern, we implement it accordingly and check the results.

## Example

C++ Code for the Above Approach


#include <bits/stdc++.h>
using namespace std;
void ansQueries(int prefeven[], int prefodd[], int l, int r){
if ((r - l + 1) % 2 == 0) // if number of element present in the range is even
cout << "0";
else{
if (l % 2 == 0) // if l is even
cout << (prefeven[r] ^ prefeven[l - 1]) << "\n";
else // if l is odd
cout << (prefodd[r] ^ prefodd[l - 1]) << "\n";
}
}
int main(){
int arr[] = {4, 1, 2, 3, 5};
int n = sizeof(arr) / sizeof(int); // size of our array
int l[] = {1, 2, 1}; // given queries' left index
int r[] = {2, 4, 4}; // given queries' right index
int q = sizeof(l) / sizeof(int);         // number of queries asked
int prefodd[n] = {0}, prefeven[n] = {0}; // different prefix xor for even and odd indices
for (int i = 1; i <= n; i++){
if ((i) % 2 == 0){ // if i is even then we change prefeven
prefeven[i] = arr[i - 1] ^ prefeven[i - 1];
prefodd[i] = prefodd[i - 1];
}else{
prefeven[i] = prefeven[i - 1];
prefodd[i] = prefodd[i - 1] ^ arr[i - 1];
}
}
for (int i = 0; i < q; i++){
ansQueries(prefeven, prefodd, l[i], r[i]);
}
return 0;
}

## Output

02
0

## Explanation of the Above Code

In this approach, we first observe that if the size of our range is even then our answer will be zero as every number appears even times when we print all the subarrays so by taking their XOR the answer simply will be 0 now for our next part where the size of the range is odd in this case the number that appears odd times are in even position in the given range and our answer will simply be the XORs of the numbers appearing in the even positions in the given range two we maintain two prefix arrays that contain the XORs of alternate positions as our prefeven contains XOR of all the even indices and prefodd contains XORs of all the odd indices now when we solve a query we simply need to check if our l is even or odd and give our answer accordingly.

## Conclusion

In this tutorial, we solve a problem to solve the Queries on XOR of XORs of all subarrays. We also learned the C++ program for this problem and the complete approach ( Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on 26-Nov-2021 05:48:39