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 {
// Driver Code
static void Main(string[] args)
{
int[] arr = { 1, 4, 7, 9, 10 };
int N = arr.Length;
oddXorSubarray(arr, N);
}
// Function to find the odd XOR subarray
static void oddXorSubarray(int[] a, int n)
{
int count = 0;
// Loop through all possible subarrays
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int xor_val = 0;
// Compute XOR of all elements
// in the subarray
for (int k = i; k <= j; k++) {
xor_val ^= a[k];
}
// If XOR value is odd, then
// increment the value of count
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the number of subarrays
// of the given array having odd Bitwise XOR
void oddXorSubarray(int a[], int n)
{
// Stores number of odd
// numbers upto i-th index
int odd = 0;
// Stores number of required
// subarrays starting from i-th index
int c_odd = 0;
// Store the required result
int result = 0;
// Find the number of subarrays having odd
// Bitwise XOR values starting at 0-th index
for (int i = 0; i < n; i++) {
// Check if current element is odd
if (a[i] & 1) {
odd = !odd;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd) {
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for (int i = 0; i < n; i++) {
// Add c_odd to result
result += c_odd;
if (a[i] & 1) {
c_odd = (n - i - c_odd);
}
}
// Print the result
cout << result;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 1, 4, 7, 9, 10 };
// Stores the size of the array
int N = sizeof(arr) / sizeof(arr[0]);
oddXorSubarray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to count the number of subarrays
// of the given array having odd Bitwise XOR
static void oddXorSubarray(int a[], int n)
{
// Stores number of odd
// numbers upto i-th index
int odd = 0;
// Stores number of required
// subarrays starting from i-th index
int c_odd = 0;
// Store the required result
int result = 0;
// Find the number of subarrays having odd
// Bitwise XOR values starting at 0-th index
for (int i = 0; i < n; i++)
{
// Check if current element is odd
if (a[i] % 2 != 0)
{
odd = (odd == 0) ? 1 : 0;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd != 0)
{
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for (int i = 0; i < n; i++)
{
// Add c_odd to result
result += c_odd;
if (a[i] % 2 != 0)
{
c_odd = (n - i - c_odd);
}
}
// Print the result
System.out.println(result);
}
// Driver Code
public static void main (String[] args)
{
// Given array
int arr[] = { 1, 4, 7, 9, 10 };
// Stores the size of the array
int N = arr.length;
oddXorSubarray(arr, N);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Function to count the number of subarrays
# of the given array having odd Bitwise XOR
def oddXorSubarray(a, n):
# Stores number of odd
# numbers upto i-th index
odd = 0
# Stores number of required
# subarrays starting from i-th index
c_odd = 0
# Store the required result
result = 0
# Find the number of subarrays having odd
# Bitwise XOR values starting at 0-th index
for i in range(n):
# Check if current element is odd
if (a[i] & 1):
odd = not odd
# If the current value of odd is not
# zero, increment c_odd by 1
if (odd):
c_odd += 1
# Find the number of subarrays having odd
# bitwise XOR value starting at ith index
# and add to result
for i in range(n):
# Add c_odd to result
result += c_odd
if (a[i] & 1):
c_odd = (n - i - c_odd)
# Print the result
print (result)
# Driver Code
if __name__ == '__main__':
# Given array
arr = [1, 4, 7, 9, 10]
# Stores the size of the array
N = len(arr)
oddXorSubarray(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of subarrays
// of the given array having odd Bitwise XOR
static void oddXorSubarray(int []a, int n)
{
// Stores number of odd
// numbers upto i-th index
int odd = 0;
// Stores number of required
// subarrays starting from i-th index
int c_odd = 0;
// Store the required result
int result = 0;
// Find the number of subarrays having
// odd Bitwise XOR values starting at
// 0-th index
for(int i = 0; i < n; i++)
{
// Check if current element is odd
if (a[i] % 2 != 0)
{
odd = (odd == 0) ? 1 : 0;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd != 0)
{
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for(int i = 0; i < n; i++)
{
// Add c_odd to result
result += c_odd;
if (a[i] % 2 != 0)
{
c_odd = (n - i - c_odd);
}
}
// Print the result
Console.WriteLine(result);
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 1, 4, 7, 9, 10 };
// Stores the size of the array
int N = arr.Length;
oddXorSubarray(arr, N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript program for the above approach
// Function to count the number of subarrays
// of the given array having odd Bitwise XOR
function oddXorSubarray(a , n) {
// Stores number of odd
// numbers upto i-th index
var odd = 0;
// Stores number of required
// subarrays starting from i-th index
var c_odd = 0;
// Store the required result
var result = 0;
// Find the number of subarrays having odd
// Bitwise XOR values starting at 0-th index
for (i = 0; i < n; i++) {
// Check if current element is odd
if (a[i] % 2 != 0) {
odd = (odd == 0) ? 1 : 0;
}
// If the current value of odd is not
// zero, increment c_odd by 1
if (odd != 0) {
c_odd++;
}
}
// Find the number of subarrays having odd
// bitwise XOR value starting at ith index
// and add to result
for (i = 0; i < n; i++) {
// Add c_odd to result
result += c_odd;
if (a[i] % 2 != 0) {
c_odd = (n - i - c_odd);
}
}
// Print the result
document.write(result);
}
// Driver Code
// Given array
var arr = [ 1, 4, 7, 9, 10 ];
// Stores the size of the array
var N = arr.length;
oddXorSubarray(arr, N);
// This code contributed by Rajput-Ji
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count subarrays having even Bitwise XOR
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: 4Explanation: The subarrays having even Bitwise XOR are {{2}, {4}, {1, 2, 3}, {1, 2, 3, 4}}. Input: arr[] = {2, 4, 6}Output: 6Expl
11 min read
Count even length subarrays having bitwise XOR equal to 0
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: 3Explanation:Subarrays having XOR of elements equal to 0 are: {{2, 2}, {3, 3}, {2, 2, 3, 3}}Therefore, the
13 min read
Count distinct Bitwise OR of all Subarrays
Given an array A of non-negative integers, where 0 \leq A[i] \leq 10^{9} . The task is to count number of distinct possible results obtained by taking the bitwise OR of all the elements in all possible Subarrays. Examples: Input: A = [1, 2] Output: 3 Explanation: The possible subarrays are [1], [2],
7 min read
Count subsequences having odd Bitwise XOR values from an array
Given an array A[] of size N, the task is to count the number of subsequences from the given array whose Bitwise XOR value is odd. Examples: Input: A[] = {1, 3, 4}Output: 4Explanation: Subsequences with odd Bitwise XOR are {1}, {3}, {1, 4}, {3, 4}. Input: A[] = {2, 8, 6}Output: 0Explanation: No such
5 min read
Count pairs with Bitwise XOR as ODD number
Given an array of N integers, the task is to find the number of pairs (i, j) such that A[i] ^ A[j] is odd.Examples: Input : N = 5 A[] = { 5, 4, 7, 2, 1} Output :6 Since pair of A[] = ( 5, 4 ) = 1( 5, 7 ) = 2( 5, 2 ) = 7( 5, 1 ) = 4 ( 4, 7 ) = 3( 4, 2 ) = 6( 4, 1 ) = 5 ( 7, 2 ) = 5( 7, 1 ) = 6 ( 2, 1
9 min read
Number of subarrays have bitwise OR >= K
Given an array arr[] and an integer K, the task is to count the number of sub-arrays having bitwise OR ? K. Examples: Input: arr[] = { 1, 2, 3 } K = 3 Output: 4Bitwise OR of sub-arrays: { 1 } = 1 { 1, 2 } = 3 { 1, 2, 3 } = 3 { 2 } = 2 { 2, 3 } = 3 { 3 } = 3 4 sub-arrays have bitwise OR ? K Input: ar
15+ min read
Bitwise AND and XOR pair counting
Given an integer array arr[] of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal. Examples: Input: N = 3, arr[] = [0, 0, 1]Output: 1Explanation: All possible pairs from the array are pair = [(0, 0), (0, 1), (1, 0)]. we can see that pair = (0, 0), 0&0 =
7 min read
Count Subarrays with given XOR
Given an array of integers arr[] and a number k, the task is to count the number of subarrays having XOR of their elements as k.Examples: Input: arr[] = [4, 2, 2, 6, 4], k = 6Output: 4Explanation: The subarrays having XOR of their elements as 6 are [4, 2], [4, 2, 2, 6, 4], [2, 2, 6], and [6].Input:
10 min read
Count pairs with Bitwise XOR as EVEN number
Given an array of N integers, the task is to find the number of pairs (i, j) such that A[i] ^ A[j] is even. Examples: Input: A[] = { 5, 4, 7, 2, 1} Output: 4 Since pair of A[] = ( 5, 4 ) = 1( 5, 7 ) = 2( 5, 2 ) = 7( 5, 1 ) = 4 ( 4, 7 ) = 3( 4, 2 ) = 6( 4, 1 ) = 5 ( 7, 2 ) = 5( 7, 1 ) = 6 ( 2, 1 ) =
11 min read
Count subsequences having odd Bitwise OR values in an array
Given an array arr[] consisting of N positive integers, the task is to find the number of subsequences from the given array whose Bitwise OR value is odd. Examples: Input: arr = [2, 4, 1]Output: 4Explanation: Subsequences with odd Bitwise OR values are {1}, {2, 1}, {4, 1}, {2, 4, 1} Input: arr = [1,
5 min read