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
def CountOfTriplets ( a , n ) : NEW_LINE INDENT answer = 0 NEW_LINE x = 0 NEW_LINE count = [ 0 for i in range ( 100005 ) ] NEW_LINE ways = [ 0 for i in range ( 100005 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT x ^= a [ i ] NEW_LINE answer += count [ x ] * i - ways [ x ] NEW_LINE count [ x ] += 1 NEW_LINE ways [ x ] += ( i + 1 ) NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Arr = [ 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 ] NEW_LINE N = len ( Arr ) NEW_LINE print ( CountOfTriplets ( Arr , N ) ) NEW_LINE DEDENT