Count subarrays having odd Bitwise XOR
Last Updated :
31 Jul, 2023
Given an array arr[] of size N, the task is to count the number of subarrays from the given array having odd Bitwise XOR value.
Examples:
Input: arr[] = {1, 4, 7, 9, 10}
Output: 8
Explanation: The subarrays having odd Bitwise XOR are {1}, {1, 4}, {1, 4, 7, 9}, {1, 4, 7, 9, 10}, {7}, {9}, {4, 7}, {9, 10}.
Input: arr[] ={1, 5, 6}
Output: 3
Explanation: The subarrays having odd Bitwise XOR are {1}, {1, 5}, {1, 5, 6}.
Brute Force Approach:
A brute force approach to solve this problem would be to generate all possible subarrays of the given array and check if the Bitwise XOR of that subarray is odd or not. If it is odd, then increment the count of the number of subarrays having odd Bitwise XOR.
Algorithm
- Initialize a variable “count” to 0 to keep track of the number of subarrays with an odd XOR.
- Loop through all possible subarrays using two nested loops.
- For each subarray, initialize a variable “xor_val” to 0
- Loop through the elements of the subarray using a third loop.
- For each element, XOR it with the current value of “xor_val”.
- Check if the XOR value of all the elements in the subarray is odd or not. If it is odd, increment the “count” variable.
- After the nested loops are complete, print the value of “count” to get the number of subarrays with an odd XOR.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void oddXorSubarray( int a[], int n)
{
int count = 0;
for ( int i=0;i<n;i++)
{
for ( int j=i;j<n;j++)
{
int xor_val = 0;
for ( int k=i;k<=j;k++)
{
xor_val ^= a[k];
}
if (xor_val % 2 != 0)
{
count++;
}
}
}
cout<< count <<endl;
}
int main()
{
int arr[] = {1, 4, 7, 9, 10};
int N = sizeof (arr)/ sizeof (arr[0]);
oddXorSubarray(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void oddXorSubarray( int [] a, int n) {
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i; j < n; j++) {
int xor_val = 0 ;
for ( int k = i; k <= j; k++) {
xor_val ^= a[k];
}
if (xor_val % 2 != 0 ) {
count++;
}
}
}
System.out.println(count);
}
public static void main(String[] args) {
int [] arr = { 1 , 4 , 7 , 9 , 10 };
int N = arr.length;
oddXorSubarray(arr, N);
}
}
|
Python3
def oddXorSubarray(a, n):
count = 0
for i in range (n):
for j in range (i, n):
xor_val = 0
for k in range (i, j + 1 ):
xor_val ^ = a[k]
if xor_val % 2 ! = 0 :
count + = 1
print (count)
arr = [ 1 , 4 , 7 , 9 , 10 ]
N = len (arr)
oddXorSubarray(arr, N)
|
C#
using System;
class Program {
static void Main( string [] args)
{
int [] arr = { 1, 4, 7, 9, 10 };
int N = arr.Length;
oddXorSubarray(arr, N);
}
static void oddXorSubarray( int [] a, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int xor_val = 0;
for ( int k = i; k <= j; k++) {
xor_val ^= a[k];
}
if (xor_val % 2 != 0) {
count++;
}
}
}
Console.WriteLine(count);
}
}
|
Javascript
function oddXorSubarray(a, n) {
let count = 0;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
let xor_val = 0;
for (let k = i; k <= j; k++) {
xor_val ^= a[k];
}
if (xor_val % 2 !== 0) {
count++;
}
}
}
console.log(count);
}
let arr = [1, 4, 7, 9, 10];
let N = arr.length;
oddXorSubarray(arr, N);
|
Time Complexity: O(N3)
Space Complexity: O(1)
Naive Approach: The simplest approach to solve this problem is to check for every subarray whether its Bitwise XOR is odd or not. If found to be odd, then increase the count. Finally, print the count as the result.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the observation that the Bitwise XOR of the subarray is odd only if the number of odd elements in that subarray is odd. To solve the problem, find the number of subarrays starting from the index 0 and satisfying the given conditions. Then, Traverse the array and update the number of subarrays starting at index i that satisfy the given condition. Follow the steps below to solve the problem:
- Initialize variables Odd, C_odd as 0, to store the number of odd numbers up to ith index and the number of required subarrays starting at ith index respectively.
- Traverse the array arr[] using the variable i and check for the following steps:
- Again, traverse the array arr[] using the variable i and perform the following:
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void oddXorSubarray( int a[], int n)
{
int odd = 0;
int c_odd = 0;
int result = 0;
for ( int i = 0; i < n; i++) {
if (a[i] & 1) {
odd = !odd;
}
if (odd) {
c_odd++;
}
}
for ( int i = 0; i < n; i++) {
result += c_odd;
if (a[i] & 1) {
c_odd = (n - i - c_odd);
}
}
cout << result;
}
int main()
{
int arr[] = { 1, 4, 7, 9, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
oddXorSubarray(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void oddXorSubarray( int a[], int n)
{
int odd = 0 ;
int c_odd = 0 ;
int result = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] % 2 != 0 )
{
odd = (odd == 0 ) ? 1 : 0 ;
}
if (odd != 0 )
{
c_odd++;
}
}
for ( int i = 0 ; i < n; i++)
{
result += c_odd;
if (a[i] % 2 != 0 )
{
c_odd = (n - i - c_odd);
}
}
System.out.println(result);
}
public static void main (String[] args)
{
int arr[] = { 1 , 4 , 7 , 9 , 10 };
int N = arr.length;
oddXorSubarray(arr, N);
}
}
|
Python3
def oddXorSubarray(a, n):
odd = 0
c_odd = 0
result = 0
for i in range (n):
if (a[i] & 1 ):
odd = not odd
if (odd):
c_odd + = 1
for i in range (n):
result + = c_odd
if (a[i] & 1 ):
c_odd = (n - i - c_odd)
print (result)
if __name__ = = '__main__' :
arr = [ 1 , 4 , 7 , 9 , 10 ]
N = len (arr)
oddXorSubarray(arr, N)
|
C#
using System;
class GFG{
static void oddXorSubarray( int []a, int n)
{
int odd = 0;
int c_odd = 0;
int result = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] % 2 != 0)
{
odd = (odd == 0) ? 1 : 0;
}
if (odd != 0)
{
c_odd++;
}
}
for ( int i = 0; i < n; i++)
{
result += c_odd;
if (a[i] % 2 != 0)
{
c_odd = (n - i - c_odd);
}
}
Console.WriteLine(result);
}
public static void Main(String[] args)
{
int []arr = { 1, 4, 7, 9, 10 };
int N = arr.Length;
oddXorSubarray(arr, N);
}
}
|
Javascript
<script>
function oddXorSubarray(a , n) {
var odd = 0;
var c_odd = 0;
var result = 0;
for (i = 0; i < n; i++) {
if (a[i] % 2 != 0) {
odd = (odd == 0) ? 1 : 0;
}
if (odd != 0) {
c_odd++;
}
}
for (i = 0; i < n; i++) {
result += c_odd;
if (a[i] % 2 != 0) {
c_odd = (n - i - c_odd);
}
}
document.write(result);
}
var arr = [ 1, 4, 7, 9, 10 ];
var N = arr.length;
oddXorSubarray(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...