You are given an array/list ‘ARR’ consisting of ‘N’ positive integers. Your task is to return the number of triplets (i, j, k), where ‘i’, ‘j’, ‘k’ are three indices of ‘ARR’ such that -:
1. 0 <= ‘i’ < ‘j’ <= ‘k’ < ‘N’ .
2. ARR[i] ^ ARR[i + 1] ^ ... ^ ARR[j - 1] = ARR[j] ^ ARR[j + 1] ^ ... ^ ARR[k] i.e bitwise xor of all integers in subarray of ‘ARR’ between index ‘i’ and ‘j-1’ inclusive, should equals to bitwise xor of all integers in subarray of ‘ARR’ between index ‘j’ and ‘k’ inclusive.
Note:
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’.
Output Format:
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.
Note:
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
2
2
1 1
5
2 3 1 6 7
1
4
In the first test case, There is one such triplet (0, 1, 1). Note xor of subarray having a single integer is that integer itself.
In the second test case, The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4).
2
5
2 2 2 2 2
2
4 6
10
0
Try out all possible ways of selecting three indices.
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:
O(N^3), where 'N' is the size of ‘ARR’.
There are three nested loops each run at most ‘N’ times. Thus, the overall time complexity will be O(N^3).
O(1)
We are using constant extra space here.