Given an array of integers Arr. The task is to count the number of triplets (i, j, k) such that Ai ^ Ai+1 ^ Ai+2 ^ …. ^ Aj-1 = Aj ^ Aj+1 ^ Aj+2 ^ ….. ^ Ak, 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 :
Ai ^ Ai + 1 ^ ...Aj - 1 = Aj ^ Aj + 1 ^ ...Ak
Taking XOR with Aj ^ Aj + 1 ^ ...Ak on both sides
Ai ^ Ai + 1 ^ ...Aj - 1 ^ Aj ^ Aj + 1 ^ ...Ak = 0
- So 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 subarrays 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 the 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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.*;
import java.util.Collections;
class GFG
{
static int CountOfTriplets( int a[], int n)
{
int answer = 0 ;
int x = 0 ;
int count[] = new int [ 100005 ];
int ways[] = new int [ 100005 ];
for ( int i = 0 ; i < n; i++)
{
x ^= a[i];
answer += count[x] * i - ways[x];
count[x]++;
ways[x] += (i + 1 );
}
return answer;
}
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));
}
}
|
Python3
def CountOfTriplets(a,n):
answer = 0
x = 0
count = [ 0 for i in range ( 100005 )]
ways = [ 0 for i in range ( 100005 )]
for i in range (n):
x ^ = a[i]
answer + = count[x] * i - ways[x]
count[x] + = 1
ways[x] + = (i + 1 )
return answer
if __name__ = = '__main__' :
Arr = [ 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 ]
N = len (Arr)
print (CountOfTriplets(Arr, N))
|
C#
using System;
class GFG {
static int CountOfTriplets( int []a, int n)
{
int answer = 0;
int x = 0;
int []count = new int [100005];
int []ways = new int [100005];
for ( int i = 0; i < n; i++)
{
x ^= a[i];
answer += count[x] * i - ways[x];
count[x]++;
ways[x] += (i + 1);
}
return answer;
}
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));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)