
If N = 5 and the array is: { 1, 3, 4, 3, 5 }, and for the given query L = 0 and R = 3, then we will return 1 ^ 3 ^ 4 ^ 3 = 5.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the size of the array.
The second line of each test case contains N positive integers denoting the array elements ‘Arr[i]’.
The third line of each test case contains a single integer ‘Q’ denoting the number of queries to be processed.
The next Q lines each contains two integers ‘L’ and ‘R’ denoting the start and end indexes of the subarray whose XOR needs to be computed.
For each test case, print Q integers, each denoting the subarray XOR for the given query.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function and return an array that stores the answer to each query.
1 <= T <= 10
1 <= N <= 100
1 <= Arr[i] <= 10^5
1 <= Q <= 200
0 <= L <= R <= N-1
Time limit: 1 sec
For each query simply run a loop and calculate the XOR of the subarray from L to R, after processing all the queries return the answer.
The steps are as follows :
We can directly get the value of the XOR of the subarray from L to R if we know the XOR value of 0 to R and the XOR value of 0 to L - 1. This is because of the fact that (x ^ y) ^ x = (x ^ x) ^ y = 0 ^ y = y.
This means XOR( L …. R ) = XOR( 0 ….. R ) ^ XOR( 0…..L-1 )
We can directly get the value of XOR( 0 ….. R ) and XOR( 0…..L-1 ) if we pre-calculate the prefix array to store XOR of elements from 0 to i at the ith position in the prefix array.
The steps are as follows :