Given an array arr[] of size N, the task is to count all possible even length subarrays having bitwise XOR of subarray elements equal to 0.
Examples:
Input: arr[] = {2, 2, 3, 3, 6, 7, 8}
Output: 3
Explanation:
Subarrays having XOR of elements equal to 0 are: {{2, 2}, {3, 3}, {2, 2, 3, 3}}
Therefore, the required output is 3.
Input: arr[] = {1, 2, 3, 3}
Output: 1
Naive Approach: The simplest approach is to traverse the array and generate all possible subarrays. For each subarray, check if the length of the subarray is even and if the Bitwise XOR of the subarray elements is 0 or not. Follow the steps below to solve the problem:
- Initialize a variable, say res to store the count of subarrays that satisfy the given condition.
- Traverse the array and generate all possible subarrays.
- For each subarray, check if the length of the subarray is even and the bitwise XOR of their elements is 0, then increment the res by 1.
- Finally, print the value of res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntSubarr( int arr[], int N)
{
int res = 0;
int prefixXor = 0;
for ( int i = 0; i < N - 1;
i++) {
prefixXor = arr[i];
for ( int j = i + 1; j < N;
j++) {
prefixXor ^= arr[j];
if (prefixXor == 0
&& (j - i + 1) % 2 == 0) {
res++;
}
}
}
return res;
}
int main()
{
int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << cntSubarr(arr, N);
}
|
C
#include <stdio.h>
int cntSubarr( int arr[], int N)
{
int res = 0;
int prefixXor = 0;
for ( int i = 0; i < N - 1;
i++) {
prefixXor = arr[i];
for ( int j = i + 1; j < N;
j++) {
prefixXor ^= arr[j];
if (prefixXor == 0
&& (j - i + 1) % 2 == 0) {
res++;
}
}
}
return res;
}
int main()
{
int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
printf ( "%d\n" , cntSubarr(arr, N));
}
|
Java
import java.io.*;
class GFG{
static int cntSubarr( int arr[], int N)
{
int res = 0 ;
int prefixXor = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
prefixXor = arr[i];
for ( int j = i + 1 ; j < N; j++)
{
prefixXor ^= arr[j];
if (prefixXor == 0 &&
(j - i + 1 ) % 2 == 0 )
{
res++;
}
}
}
return res;
}
public static void main (String[] args)
{
int arr[] = { 2 , 2 , 3 , 3 , 6 , 7 , 8 };
int N = arr.length;
System.out.println(cntSubarr(arr, N));
}
}
|
Python3
def cntSubarr(arr, N):
res = 0
prefixXor = 0
for i in range (N - 1 ):
prefixXor = arr[i]
for j in range (i + 1 , N):
prefixXor ^ = arr[j]
if (prefixXor = = 0 and
(j - i + 1 ) % 2 = = 0 ):
res + = 1
return res
if __name__ = = '__main__' :
arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ]
N = len (arr)
print (cntSubarr(arr, N))
|
C#
using System;
class GFG{
static int cntSubarr( int [] arr, int N)
{
int res = 0;
int prefixXor = 0;
for ( int i = 0; i < N - 1; i++)
{
prefixXor = arr[i];
for ( int j = i + 1; j < N; j++)
{
prefixXor ^= arr[j];
if (prefixXor == 0 &&
(j - i + 1) % 2 == 0)
{
res++;
}
}
}
return res;
}
public static void Main ()
{
int [] arr = { 2, 2, 3, 3, 6, 7, 8 };
int N = arr.Length;
Console.WriteLine(cntSubarr(arr, N));
}
}
|
Javascript
<script>
function cntSubarr(arr, N)
{
var res = 0;
var prefixXor = 0;
var i,j;
for (i = 0; i < N - 1;
i++) {
prefixXor = arr[i];
for (j = i + 1; j < N;
j++) {
prefixXor ^= arr[j];
if (prefixXor == 0
&& (j - i + 1) % 2 == 0) {
res++;
}
}
}
return res;
}
var arr = [2, 2, 3, 3, 6, 7, 8];
var N = arr.length;
document.write(cntSubarr(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved using Hashing. The idea is to store the frequency of the Prefix Xor in two separate arrays, say Odd[] and Even[], to store the frequency of the Prefix XOR of odd and even indices of the given array. Finally, print the count of all possible pairs from Even[] and Odd[] arrays having a value greater than or equal to 2. Following are the observations:
Odd Index – Odd Index = Even Length
Even Index – Even Index = Even Length
If Even[X] ≥ 2: Bitwise XOR of all the elements between two even indices of the given array must be 0 and the length of the subarray is also an even number ( Even Index – Even Index ).
If Odd[X] ≥ 2: Bitwise XOR of all the elements between two odd indices of the given array must be 0 and the length of the subarray is also an even number ( Odd Index – Odd Index ).
Follow the steps below to solve the problem:
- Initialize two arrays, say Even[] and Odd[] to store the frequency of Prefix XOR at even and odd indices of the given array respectively.
- Initialize a variable, say cntSub, to store the count of subarrays that satisfy the given condition.
- Traverse the given array and compute the Prefix Xor of the given array.
- Store the frequency of Prefix XOR at even and odd indices of the given array in the arrays Even[] and Odd[] respectively.
- Finally, print the count of all possible pairs of Even[] and Odd[] having a value greater than or equal to 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define M 1000000
int cntSubXor( int arr[], int N)
{
int prefixXor = 0;
int Even[M];
int Odd[M];
int cntSub = 0;
Odd[0] = 1;
for ( int i = 0; i < N; i++) {
prefixXor ^= arr[i];
if (i % 2 == 1) {
cntSub += Odd[prefixXor];
Odd[prefixXor]++;
}
else {
cntSub += Even[prefixXor];
Even[prefixXor]++;
}
}
return cntSub;
}
int main()
{
int arr[] = { 2, 2, 3, 3, 6, 7, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << cntSubXor(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int M = 1000000 ;
static int cntSubXor( int arr[], int N)
{
int prefixXor = 0 ;
int []Even = new int [M];
int []Odd = new int [M];
int cntSub = 0 ;
Odd[ 0 ] = 1 ;
for ( int i = 0 ; i < N; i++)
{
prefixXor ^= arr[i];
if (i % 2 == 1 )
{
cntSub += Odd[prefixXor];
Odd[prefixXor]++;
}
else
{
cntSub += Even[prefixXor];
Even[prefixXor]++;
}
}
return cntSub;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 3 , 3 , 6 , 7 , 8 };
int N = arr.length;
System.out.print(cntSubXor(arr, N));
}
}
|
Python3
M = 1000000 ;
def cntSubXor(arr, N):
prefixXor = 0 ;
Even = [ 0 ] * M;
Odd = [ 0 ] * M;
cntSub = 0 ;
Odd[ 0 ] = 1 ;
for i in range ( 0 , N):
prefixXor ^ = arr[i];
if (i % 2 = = 1 ):
cntSub + = Odd[prefixXor];
Odd[prefixXor] + = 1 ;
else :
cntSub + = Even[prefixXor];
Even[prefixXor] + = 1 ;
return cntSub;
if __name__ = = '__main__' :
arr = [ 2 , 2 , 3 , 3 ,
6 , 7 , 8 ];
N = len (arr);
print (cntSubXor(arr, N));
|
C#
using System;
class GFG{
static readonly int M = 1000000;
static int cntSubXor( int []arr, int N)
{
int prefixXor = 0;
int []Even = new int [M];
int []Odd = new int [M];
int cntSub = 0;
Odd[0] = 1;
for ( int i = 0; i < N; i++)
{
prefixXor ^= arr[i];
if (i % 2 == 1)
{
cntSub += Odd[prefixXor];
Odd[prefixXor]++;
}
else
{
cntSub += Even[prefixXor];
Even[prefixXor]++;
}
}
return cntSub;
}
public static void Main(String[] args)
{
int []arr = { 2, 2, 3, 3, 6, 7, 8 };
int N = arr.Length;
Console.Write(cntSubXor(arr, N));
}
}
|
Javascript
<script>
let M = 1000000;
function cntSubXor(arr, N)
{
let prefixXor = 0;
let Even = Array.from({length: M}, (_, i) => 0);
let Odd = Array.from({length: M}, (_, i) => 0);
let cntSub = 0;
Odd[0] = 1;
for (let i = 0; i < N; i++)
{
prefixXor = Math.floor(prefixXor ^ arr[i]);
if (i % 2 == 1)
{
cntSub += Odd[prefixXor];
Odd[prefixXor]++;
}
else
{
cntSub += Even[prefixXor];
Even[prefixXor]++;
}
}
return cntSub;
}
let arr = [ 2, 2, 3, 3, 6, 7, 8 ];
let N = arr.length;
document.write(cntSubXor(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(M), where M is the maximum bitwise XOR possible in all subarrays.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!