Count triplet of indices ( i , j , k ) such that XOR of elements between [ i , j ) equals [ j , k ] | C ++ program to count the Number of triplets in array having subarray XOR equal ; Function return the count of triplets having subarray XOR equal ; XOR value till i ; Count and ways array as defined above ; Using the formula stated ; Increase the frequency of x ; Add i + 1 to ways [ x ] for upcoming indices ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountOfTriplets ( int a [ ] , int n ) { int answer = 0 ; int x = 0 ; int count [ 100005 ] = { 0 } ; int ways [ 100005 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { x ^= a [ i ] ; answer += count [ x ] * i - ways [ x ] ; count [ x ] ++ ; ways [ x ] += ( i + 1 ) ; } return answer ; } int main ( ) { int Arr [ ] = { 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 } ; int N = sizeof ( Arr ) / sizeof ( Arr [ 0 ] ) ; cout << CountOfTriplets ( Arr , N ) ; return 0 ; }