Given an array of integers **Arr**. The task is to count the number of triplets **(i, j, k)** such that **A _{i} ^ A_{i+1} ^ A_{i+2} ^ …. ^ A_{j-1} = A_{j} ^ A_{j+1} ^ A_{j+2} ^ ….. ^ A_{k}**, and

**0 < (i, j, k) < N**, where

**N**is the size of the array.

Where **^** is the **bitwise xor** of two numbers.

**Examples:**

Input:Arr = [5, 2, 7]

Output:2

Explanation:

The triplets are (0, 2, 2) since 5 ^ 2 = 7 and (0, 1, 2) since 2 ^ 7 = 5.

Input:Arr = [3, 6, 12, 8, 6, 2, 1, 5]

Output:6

**Approach**

- Let’s simplify the given expression :
A

_{i}^ A_{i + 1}^ ...A_{j - 1}= A_{j}^ A_{j + 1}^ ...A_{k}Taking XOR with A_{j}^ A_{j + 1}^ ...A_{k}on both sides A_{i}^ A_{i + 1}^ ...A_{j - 1}^ A_{j}^ A_{j + 1}^ ...A_{k}= 0So a subarray [i, k] having XOR 0 will have

**k – i**triplets, because any j can be selected from [i+1, k] and the triplet will satisfy the condition. - The problem now reduces to finding lengths of all subarray whose XOR is 0 and for each such length L, add L – 1 to the answer.
- In the prefix XOR array if at 2 indices, say L and R, the prefix XOR value is same – then it means that the XOR of subarray [L + 1, R] = 0.
- To find the count, we will store the following :
Say we are at index i, and the prefix XOR at i = x. count[x] = Frequency of x in prefix XOR array before i ways[x] -> For each all j < i, if prefixXor[j] = x, then ways[x] += (j+1)

These can be used to count the triplets using the formula :

Triplets += count[x] * i - ways[x]

Below is the implementation of the above approach:

## C++

`// C++ program to count the Number of ` `// triplets in array having subarray ` `// XOR equal ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function return the count of ` `// triplets having subarray XOR equal ` `int` `CountOfTriplets(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `answer = 0; ` ` ` ` ` `// XOR value till i ` ` ` `int` `x = 0; ` ` ` ` ` `// Count and ways array as defined ` ` ` `// above ` ` ` `int` `count[100005] = { 0 }; ` ` ` `int` `ways[100005] = { 0 }; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `x ^= a[i]; ` ` ` ` ` `// Using the formula stated ` ` ` `answer += count[x] * i - ways[x]; ` ` ` ` ` `// Increase the frequency of x ` ` ` `count[x]++; ` ` ` ` ` `// Add i+1 to ways[x] for upcoming ` ` ` `// indices ` ` ` `ways[x] += (i + 1); ` ` ` `} ` ` ` `return` `answer; ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the Number of ` `// triplets in array having subarray ` `// XOR equal ` `import` `java.io.*; ` `import` `java.util.Arrays; ` `import` `java.util.ArrayList; ` `import` `java.lang.*; ` `import` `java.util.Collections; ` ` ` `class` `GFG ` `{ ` ` ` `// Function return the count of ` `// triplets having subarray XOR equal ` `static` `int` `CountOfTriplets(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `answer = ` `0` `; ` ` ` ` ` `// XOR value till i ` ` ` `int` `x = ` `0` `; ` ` ` ` ` `// Count and ways array as defined ` ` ` `// above ` ` ` `int` `count[] = ` `new` `int` `[` `100005` `]; ` ` ` `int` `ways[] = ` `new` `int` `[` `100005` `]; ` ` ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `x ^= a[i]; ` ` ` ` ` `// Using the formula stated ` ` ` `answer += count[x] * i - ways[x]; ` ` ` ` ` `// Increase the frequency of x ` ` ` `count[x]++; ` ` ` ` ` `// Add i+1 to ways[x] for upcoming ` ` ` `// indices ` ` ` `ways[x] += (i + ` `1` `); ` ` ` `} ` ` ` `return` `answer; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `int` `Arr[] = { ` `3` `, ` `6` `, ` `12` `, ` `8` `, ` `6` `, ` `2` `, ` `1` `, ` `5` `}; ` ` ` ` ` `int` `N = Arr.length; ` ` ` ` ` `System.out.print(CountOfTriplets(Arr, N)); ` ` ` `} ` `} ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the Number of ` `# triplets in array having subarray ` `# XOR equal ` ` ` `# Function return the count of ` `# triplets having subarray XOR equal ` `def` `CountOfTriplets(a,n): ` ` ` `answer ` `=` `0` ` ` ` ` `# XOR value till i ` ` ` `x ` `=` `0` ` ` ` ` `# Count and ways array as defined ` ` ` `# above ` ` ` `count ` `=` `[` `0` `for` `i ` `in` `range` `(` `100005` `)] ` ` ` `ways ` `=` `[` `0` `for` `i ` `in` `range` `(` `100005` `)] ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `x ^` `=` `a[i] ` ` ` ` ` `# Using the formula stated ` ` ` `answer ` `+` `=` `count[x] ` `*` `i ` `-` `ways[x] ` ` ` ` ` `# Increase the frequency of x ` ` ` `count[x] ` `+` `=` `1` ` ` ` ` `# Add i+1 to ways[x] for upcoming ` ` ` `# indices ` ` ` `ways[x] ` `+` `=` `(i ` `+` `1` `) ` ` ` `return` `answer ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `Arr ` `=` `[` `3` `, ` `6` `, ` `12` `, ` `8` `, ` `6` `, ` `2` `, ` `1` `, ` `5` `] ` ` ` ` ` `N ` `=` `len` `(Arr) ` ` ` ` ` `print` `(CountOfTriplets(Arr, N)) ` ` ` `# This code is contributed by Bhupendra_Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the Number of ` `// triplets in array having subarray ` `// XOR equal ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function return the count of ` `// triplets having subarray XOR equal ` `static` `int` `CountOfTriplets(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `int` `answer = 0; ` ` ` ` ` `// XOR value till i ` ` ` `int` `x = 0; ` ` ` ` ` `// Count and ways array as defined ` ` ` `// above ` ` ` `int` `[]count = ` `new` `int` `[100005]; ` ` ` `int` `[]ways = ` `new` `int` `[100005]; ` ` ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `x ^= a[i]; ` ` ` ` ` `// Using the formula stated ` ` ` `answer += count[x] * i - ways[x]; ` ` ` ` ` `// Increase the frequency of x ` ` ` `count[x]++; ` ` ` ` ` `// Add i+1 to ways[x] for upcoming ` ` ` `// indices ` ` ` `ways[x] += (i + 1); ` ` ` `} ` ` ` ` ` `return` `answer; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `int` `[]Arr = { 3, 6, 12, 8, 6, 2, 1, 5 }; ` ` ` `int` `N = Arr.Length; ` ` ` ` ` `Console.Write(CountOfTriplets(Arr, N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rohit_ranjan ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity :** O(N)

## Recommended Posts:

- Find smallest number n such that n XOR n+1 equals to given k.
- Count the pairs in an array such that the difference between them and their indices is equal
- Array containing power of 2 whose XOR and Sum of elements equals X
- Minimize count of unequal elements at corresponding indices between given arrays
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Choose X such that (A xor X) + (B xor X) is minimized
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
- Check if maximum difference between indices of Non-Zero Elements is greater than X
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Value to be subtracted from array elements to make sum of all elements equals K
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Permutation of first N positive integers such that prime numbers are at prime indices
- Maximize the number of indices such that element is greater than element to its left
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Largest subsequence such that all indices and all values are multiples individually
- Count of elements which are equal to the XOR of the next two elements
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.