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}.
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:
- If the value of arr[i] is odd, update the value of Odd to !Odd.
- If the value of Odd is non-zero, increment C_odd by 1.
- Again, traverse the array arr[] using the variable i and perform the following:
- Add the value of C_odd to a variable res.
- If the value of arr[i] is odd, update the value of C_odd to (N – i – C_odd).
- 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; } |
8
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

