Count triplet of indices ( i , j , k ) such that XOR of elements between [ i , j ) equals [ j , k ] | 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
function CountOfTriplets ( a , n ) { let answer = 0 ; let x = 0 ; let count = new Array ( 100005 ) ; let ways = new Array ( 100005 ) ; count . fill ( 0 ) ; ways . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { x ^= a [ i ] ; answer += count [ x ] * i - ways [ x ] ; count [ x ] ++ ; ways [ x ] = ways [ x ] + i + 1 ; } return answer ; } let Arr = [ 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 ] ; let N = Arr . length ; document . write ( CountOfTriplets ( Arr , N ) ) ;