
1. Consider 0 based indexing in ‘ARR’.
2. Symbol ^ denotes the bitwise-xor operation.
3. Xor of subarray having a single integer is that integer itself.
4. It is guaranteed that the number of such triples fit in a 32 bit integer.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single integer ‘N’, representing the size of ‘ARR’
The second line of each test case will contain ‘N’ space-separated integers representing array/list ‘ARR’.
For each test case, print a single integer that represents the number of such triplets.
Output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of.
1 <= T <= 50
2 <= N <= 10000
1 <= ARR[i] <= 2^31 - 1
Time limit: 1 sec
The basic idea is to initialize ‘COUNTER’:= 0 and then one by one try out all possible ways of selecting three indices, if for any triplet (i, j, k), Xor of subarray ‘ARR[i:j-1]’ equal Xor of subarray ‘ARR[j:k]’, then increment ‘COUNTER’ by 1.
The steps are as follows:
The basic idea is based on the fact that xor of two equal integers is 0 and two integers are equal if their xor is 0. So, we can say for any triplet (i, j, k) such that 0 <= ‘i’ < ‘j’ <= ‘k’ < ‘N’ and ‘ARR[i]’ ^ ‘ARR[i + 1]’ ^ ... ^ ‘ARR[j - 1]’ = ‘ARR[j]’ ^ ‘ARR[j + 1]’ ^ ... ^ ‘ARR[k]’ , then ‘ARR[i]’ ^ ‘ARR[i + 1]’ ^ ... ‘ARR[k]’ = 0, i.e xor of all integer in subarray ‘ARR[i:k]’ is 0.
For any subarray ‘ARR[i:k]’ if xor of its all elements is 0, then for any integer ‘j’ such that ‘i’ < ‘j’ <= ‘k’, we can say, xor of all elements of subarray ‘ARR[i:j-1]’ equals xor of all elements of subarray ‘ARR[j:k]’, that means every such pair (‘i’ , ‘k’) can split in ‘k-i’ valid triplets (‘i’ , ‘i+1’ , ‘k’), (‘i’ , ‘i+2’ , ‘k’) .. (‘i’ , ‘k-1’ , ‘k’), (‘i’ , ‘k’ , ‘k’). Thus the problem reduces to identifying all subarrays having xor 0 and the answer will be the sum of the length of all such subarrays., we can easily find all such subarrays in O(N^2) times.
The steps are as follows:
As discussed in the previous approach, we need to find all such pair (i, k) such that xor of all integers of subarray ‘ARR[i:k]’ is 0 i.e ‘ARR[i]’ ^ ‘ARR[i+1]’ ^...^ ‘ARR[k]’ = 0 and answer will be the sum of ‘k-i’ for each such pair.
To find out the pairs (‘i’ , ‘k’) that satisfy ‘ARR[i]’ ^ ‘ARR[i+1]’ ^...^ ‘ARR[k]’ = 0, we can use a map to store the xor of prefixes, and when xor of two prefixes are equal, like ‘ARR[0]’ ^ ‘ARR[1]’ ^...^ ‘ARR[i-1]’ = ‘ARR[0]’ ^ ‘ARR[1]’ ^...^ ‘ARR[k]’, we can conclude that (‘ARR[0]’ ^ ‘ARR[1]’ ^...^ ‘ARR[i-1]’ ) ^ (‘ARR[0]’ ^ ‘ARR[1]’ ^...^ ‘ARR[k])’ = ‘ARR[i]’ ^ ‘ARR[i+1]’ ^...^ ‘ARR[k]’ = 0. In other words, xor of all integers of subarray ‘ARR[i:j]’ will be 0 if xor of all integers of prefix ‘ARR[0:i-1]’ and prefix ‘ARR[0:j]’ is equal.
If we check the prefixes with the same xor-sum every time, then the time complexity is O(N^2), However, because only the sum of starts of the prefixes and the number of prefixes are important, we can optimize the solution to O(N) using Hashmap as described below.
The steps are as follows: