Count subarrays having even Bitwise XOR
Last Updated :
05 Jul, 2022
Given an array arr[] of size N, the task is to count the number of subarrays from the given array whose Bitwise XOR is even.
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 4
Explanation: The subarrays having even Bitwise XOR are {{2}, {4}, {1, 2, 3}, {1, 2, 3, 4}}.
Input: arr[] = {2, 4, 6}
Output: 6
Explanation: The subarrays having even Bitwise XOR are {{2}, {4}, {6}, {2, 4}, {4, 6}, {2, 4, 6}}.
Naive Approach: The simplest approach to solve this problem is to generate all possible subarrays and check for every subarray, whether Bitwise XOR of all its elements are even or not. If found to be even, then increase the count. Finally, print the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void evenXorSubarray( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int XOR = 0;
for ( int j = i; j < n; j++) {
XOR = XOR ^ arr[j];
if ((XOR & 1) == 0)
ans++;
}
}
cout << ans;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
evenXorSubarray(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void evenXorSubarray( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
int XOR = 0 ;
for ( int j = i; j < n; j++) {
XOR = XOR ^ arr[j];
if ((XOR & 1 ) == 0 )
ans++;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
evenXorSubarray(arr, N);
}
}
|
Python3
def evenXorSubarray(arr, n):
ans = 0
for i in range (n):
XOR = 0
for j in range (i, n):
XOR = XOR ^ arr[j]
if ((XOR & 1 ) = = 0 ):
ans + = 1
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
evenXorSubarray(arr, N)
|
C#
using System;
class GFG
{
static void evenXorSubarray( int [] arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int XOR = 0;
for ( int j = i; j < n; j++) {
XOR = XOR ^ arr[j];
if ((XOR & 1) == 0)
ans++;
}
}
Console.WriteLine(ans);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
evenXorSubarray(arr, N);
}
}
|
Javascript
<script>
function evenXorSubarray(arr, n)
{
let ans = 0;
for (let i = 0; i < n; i++) {
let XOR = 0;
for (let j = i; j < n; j++) {
XOR = XOR ^ arr[j];
if ((XOR & 1) == 0)
ans++;
}
}
document.write(ans);
}
let arr = [ 1, 2, 3, 4 ];
let N = arr.length;
evenXorSubarray(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on following observations:
Bitwise XOR of all elements in the range of indices [i + 1, j] be A.
Bitwise XOR of all elements in the range of indices [0, i] be B.
Bitwise XOR of all elements in the range of indices [0, j] be C.
By performing B ^ C, the common elements from the two ranges cancel out, resulting in XOR of all elements from the range [i + 1, j].
Therefore, the idea is to update the number of subarrays starting from index 0, having even and odd XOR values while traversing the array and updating the count accordingly. Follow the steps below to solve the problem:
- Initialize two variables, say ans and XOR, to store the required count of subarrays and store XOR values of subarrays respectively.
- Initialize an array, say freq[] of size 2, where freq[0] denotes the number of subarrays having even XOR value, and freq[1] denotes the number of subarrays having odd XOR values, starting from the index 0.
- Traverse the array, arr[] using a variable, say i, and for each array element:
- Update the value of XOR to XOR ^ arr[i].
- If XOR is even, then increase the value of ans by 1 + freq[0].
- Increment freq[0] by 1, because when subarray {arr[0], i] is XORed with other subarrays having even xor value then it would result in an even XOR value. Also, 1 is added to consider the entire subarray[0, i].
- Similarly, if XOR is odd, increment ans by freq[1] and increment freq[1] by 1.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void evenXorSubarray( int arr[], int n)
{
int ans = 0;
int freq[] = { 0, 0 };
int XOR = 0;
for ( int i = 0; i < n; i++) {
XOR = XOR ^ arr[i];
if (XOR % 2 == 0) {
ans += freq[0] + 1;
freq[0]++;
}
else {
ans += freq[1];
freq[1]++;
}
}
cout << ans;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
evenXorSubarray(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void evenXorSubarray( int arr[], int n)
{
int ans = 0 ;
int freq[] = { 0 , 0 };
int XOR = 0 ;
for ( int i = 0 ; i < n; i++) {
XOR = XOR ^ arr[i];
if (XOR % 2 == 0 ) {
ans += freq[ 0 ] + 1 ;
freq[ 0 ]++;
}
else {
ans += freq[ 1 ];
freq[ 1 ]++;
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
evenXorSubarray(arr, N);
}
}
|
Python3
def evenXorSubarray(arr, n):
ans = 0
freq = [ 0 ] * n
XOR = 0
for i in range (n):
XOR = XOR ^ arr[i]
if (XOR % 2 = = 0 ):
ans + = freq[ 0 ] + 1
freq[ 0 ] + = 1
else :
ans + = freq[ 1 ]
freq[ 1 ] + = 1
print (ans)
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
evenXorSubarray(arr, N)
|
C#
using System;
class GFG
{
static void evenXorSubarray( int [] arr, int n)
{
int ans = 0;
int [] freq = { 0, 0 };
int XOR = 0;
for ( int i = 0; i < n; i++) {
XOR = XOR ^ arr[i];
if (XOR % 2 == 0) {
ans += freq[0] + 1;
freq[0]++;
}
else {
ans += freq[1];
freq[1]++;
}
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
evenXorSubarray(arr, N);
}
}
|
Javascript
<script>
function evenXorSubarray(arr, n)
{
let ans = 0;
let freq = [ 0, 0 ];
let XOR = 0;
for (let i = 0; i < n; i++) {
XOR = XOR ^ arr[i];
if (XOR % 2 == 0) {
ans += freq[0] + 1;
freq[0]++;
}
else {
ans += freq[1];
freq[1]++;
}
}
document.write(ans);
}
let arr = [ 1, 2, 3, 4 ];
let N = arr.length;
evenXorSubarray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...