Find number of subarrays with XOR value a power of 2
Given an integer array, arr[] of size N. The XOR value of any subarray of arr[] is defined as the xor of all the integers in that subarray. The task is to find the number of sub-arrays with XOR value a power of 2. (1, 2, 4, 8, 16, ….)
Examples:
Input : arr[] = {2, 6, 7, 5, 8}
Output : 6
Subarrays : {2, 6}, {2}, {6, 7}, {6, 7, 5}, {7, 5}, {8}
Input : arr[] = {2, 4, 8, 16}
Output : 4
Approach :
- Maintain a hashmap which stores all the prefix XOR values and their counts.
- At any index i, we need to find the number of subarrays which end at i and have XOR value a power of 2. We need to find for all the power of 2, the number of subarrays possible. For example. : Suppose current XOR value till index i is X, we need to find the number of subarrays which result in 16 (say S), so we need the count of prefix XOR Y such that (X^Y) = S or Y = S^X. Y can be find using Hash Map.
- Perform Step 2, for all the index, add the output.
Below is the implementation of the above approach:
C++
// C++ Program to count number of subarrays // with Bitwise-XOR as power of 2 #include <bits/stdc++.h> #define ll long long int #define MAX 10 using namespace std; // Function to find number of subarrays int findSubarray(int array[], int n) { // Hash Map to store prefix XOR values unordered_map<int, int> mp; // When no element is selected mp.insert({ 0, 1 }); int answer = 0; int preXor = 0; for (int i = 0; i < n; i++) { int value = 1; preXor ^= array[i]; // Check for all the powers of 2, // till a MAX value for (int j = 1; j <= MAX; j++) { int Y = value ^ preXor; if (mp.find(Y) != mp.end()) { answer += mp[Y]; } value *= 2; } // Insert Current prefixxor in Hash Map if (mp.find(preXor) != mp.end()) { mp[preXor]++; } else { mp.insert({ preXor, 1 }); } } return answer; } // Driver Code int main() { int array[] = { 2, 6, 7, 5, 8 }; int n = sizeof(array) / sizeof(array[0]); cout << findSubarray(array, n) << endl; return 0; } |
chevron_right
filter_none
Java
// Java Program to count number of subarrays // with Bitwise-XOR as power of 2 import java.util.*; class GFG { static int MAX = 10; // Function to find number of subarrays static int findSubarray(int array[], int n) { // Hash Map to store prefix XOR values HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); // When no element is selected mp.put(0, 1); int answer = 0; int preXor = 0; for (int i = 0; i < n; i++) { int value = 1; preXor ^= array[i]; // Check for all the powers of 2, // till a MAX value for (int j = 1; j <= MAX; j++) { int Y = value ^ preXor; if (mp.containsKey(Y)) { answer += mp.get(Y); } value *= 2; } // Insert Current prefixxor in Hash Map if (mp.containsKey(preXor)) { mp.put(preXor,mp.get(preXor) + 1); } else { mp.put(preXor, 1); } } return answer; } // Driver Code public static void main (String[] args) { int array[] = { 2, 6, 7, 5, 8 }; int n = array.length; System.out.println(findSubarray(array, n)); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
Python3
# Python3 Program to count number of subarrays # with Bitwise-XOR as power of 2 MAX = 10 # Function to find number of subarrays def findSubarray(array, n): # Hash Map to store prefix XOR values mp = dict() # When no element is selected mp[0] = 1 answer = 0 preXor = 0 for i in range(n): value = 1 preXor ^= array[i] # Check for all the powers of 2, # till a MAX value for j in range(1, MAX + 1): Y = value ^ preXor if ( Y in mp.keys()): answer += mp[Y] value *= 2 # Insert Current prefixxor in Hash Map if (preXor in mp.keys()): mp[preXor] += 1 else: mp[preXor] = 1 return answer # Driver Code array = [2, 6, 7, 5, 8] n = len(array) print(findSubarray(array, n)) # This code is contributed by Mohit Kumar |
chevron_right
filter_none
C#
// C# Program to count number of subarrays // with Bitwise-XOR as power of 2 using System; using System.Collections.Generic; class GFG { static int MAX = 10; // Function to find number of subarrays static int findSubarray(int []array, int n) { // Hash Map to store prefix XOR values Dictionary<int, int> mp = new Dictionary<int, int>(); // When no element is selected mp.Add(0, 1); int answer = 0; int preXor = 0; for (int i = 0; i < n; i++) { int value = 1; preXor ^= array[i]; // Check for all the powers of 2, // till a MAX value for (int j = 1; j <= MAX; j++) { int Y = value ^ preXor; if (mp.ContainsKey(Y)) { answer += mp[Y]; } value *= 2; } // Insert Current prefixxor in Hash Map if (mp.ContainsKey(preXor)) { mp[preXor] = mp[preXor] + 1; } else { mp.Add(preXor, 1); } } return answer; } // Driver Code public static void Main (String[] args) { int []array = { 2, 6, 7, 5, 8 }; int n = array.Length; Console.WriteLine(findSubarray(array, n)); } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output:
6
Recommended Posts:
- Find number of subarrays with even sum
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Find whether a given number is a power of 4 or not
- Check if given number is a power of d where d is a power of 2
- Number of subarrays having sum less than K
- Find if array can be divided into two subarrays of equal sum
- Find the count of Strictly decreasing Subarrays
- Number of subarrays have bitwise OR >= K
- Maximize the number of subarrays with XOR as zero
- Number of subarrays having sum of the form k^m, m >= 0
- Number of subarrays with m odd numbers
- Number of subarrays having product less than K
- Count the number of subarrays having a given XOR
- Number of subarrays with given product
- Number of subarrays having sum in a given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



