Open In App

Count Subarrays with given XOR

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 = 6
Output: 4
Explanation: The subarrays having XOR of their elements as 6 are [4, 2], [4, 2, 2, 6, 4], [2, 2, 6], and [6].

Input: arr[] = [5, 6, 7, 8, 9], k = 5
Output: 2
Explanation: The subarrays having XOR of their elements as 5 are [5] and [5, 6, 7, 8, 9].

[Naive Approach] Checking all Subarray – O(n^2) Time and O(1) Space

A Simple Solution is to use two loops to go through all possible subarrays of arr[] and count the number of subarrays having XOR of their elements as k

C++
// C++ Program to count all subarrays having
// XOR of elements as given value K
#include <iostream>
#include <vector>
using namespace std;

// Function to find count of subarrays of arr
// with XOR value equals to K
int subarrayXor(vector<int>& arr, int k) {
    int res = 0; 

    // Pick starting point i of subarrays
    for (int i = 0; i < arr.size(); i++) {
        int prefXOR = 0; 

        // Pick ending point j of subarray for each i
        for (int j = i; j < arr.size(); j++) {
          
            // calculate prefXOR if subarray arr[i...j]
            prefXOR = prefXOR ^ arr[j];

            // If prefXOR is equal to given value,
            // increase ans by 1.
            if (prefXOR == k)
                res++;
        }
    }
    return res;
}

int main() {
    vector<int> arr = { 4, 2, 2, 6, 4 };
    int k = 6;

    cout << subarrayXor(arr, k);
    return 0;
}
Java
// Java Program to count all subarrays having
// XOR of elements as given value K
class GfG {
  
    // Function to find count of subarrays of arr 
  	// with XOR value equals to K
    static int subarrayXor(int[] arr, int k) {
        int res = 0;

        // Pick starting point i of subarrays
        for (int i = 0; i < arr.length; i++) {
            int prefXOR = 0;

            // Pick ending point j of subarray for each i
            for (int j = i; j < arr.length; j++) {
                // calculate prefXOR for subarray arr[i ... j]
                prefXOR = prefXOR ^ arr[j];

                // If prefXOR is equal to given value, increase res by 1
                if (prefXOR == k)
                    res++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = { 4, 2, 2, 6, 4 };
        int k = 6;

        System.out.println(subarrayXor(arr, k));
    }
}
Python
# Python Program to count all subarrays having
# XOR of elements as given value K

def subarrayXor(arr, k):
    res = 0

    # Pick starting point i of subarrays
    for i in range(len(arr)):
        prefXOR = 0

        # Pick ending point j of subarray for each i
        for j in range(i, len(arr)):
            # calculate prefXOR for subarray arr[i ... j]
            prefXOR ^= arr[j]

            # If prefXOR is equal to given value, increase res by 1
            if prefXOR == k:
                res += 1

    return res

if __name__ == "__main__":
    arr = [4, 2, 2, 6, 4]
    k = 6

    print(subarrayXor(arr, k))
C#
// C# Program to count all subarrays having
// XOR of elements as given value K
using System;

class GfG {
    // Function to find count of subarrays of arr 
  	// with XOR value equals to K
    static int subarrayXor(int[] arr, int k) {
        int res = 0;

        // Pick starting point i of subarrays
        for (int i = 0; i < arr.Length; i++) {
            int prefXOR = 0;

            // Pick ending point j of subarray for each i
            for (int j = i; j < arr.Length; j++) {
              
                // calculate prefXOR for subarray arr[i ... j]
                prefXOR ^= arr[j];

                // If prefXOR is equal to given value, increase res by 1
                if (prefXOR == k)
                    res++;
            }
        }
        return res;
    }

    static void Main(string[] args) {
        int[] arr = { 4, 2, 2, 6, 4 };
        int k = 6;

        Console.WriteLine(subarrayXor(arr, k));
    }
}
JavaScript
// JavaScript Program to count all subarrays having
// XOR of elements as given value K

function subarrayXor(arr, k) {
    let res = 0;

    // Pick starting point i of subarrays
    for (let i = 0; i < arr.length; i++) {
        let prefXOR = 0;

        // Pick ending point j of subarray for each i
        for (let j = i; j < arr.length; j++) {
        
            // calculate prefXOR for subarray arr[i ... j]
            prefXOR ^= arr[j];

            // If prefXOR is equal to given value, increase res by 1
            if (prefXOR === k)
                res++;
        }
    }
    return res;
}

// Driver Code
const arr = [4, 2, 2, 6, 4];
const k = 6;

console.log(subarrayXor(arr, k));

Output
4

[Efficient Approach] Using Hash Map and Prefix Sum – O(n) Time and O(n) Space

The idea is to use the properties of XOR. Let’s denote the XOR of all elements in the range [0, i] as A, the XOR of all elements in the range [i+1, j] as B, and the XOR of all elements in the range [0, j] as C.

From the properties of XOR: C = A ⊕ B
This implies: A = C ⊕ B

Now if we know the value of C (the XOR of the prefix up to index j) and have B (the target XOR value k), we can determine A (the XOR of the prefix up to index i) using: A = C ⊕ B

Using this relation, if a prefix sum A has already been seen earlier, it means there exists a subarray ending at j whose XOR is equal to k (B).

Step by step Approach:

  1. Use a hash map to store the frequency of prefix XOR values encountered so far.
  2. Traverse through the array and for each element at index i:
    • Update prefXOR = prefXOR ⊕ arr[i].
    • Check if A = prefXOR ⊕ k exists in the hash map. If it does, add the value of the hash map entry to result as it gives the count of subarrays ending at the current index that have XOR equal to k.
    • If prefXOR is equal to k, increment the result directly, as this indicates the subarray arr[0, i] has XOR equal to k.
    • Update the hash map to include the current prefXOR value.


C++
// C++ Program to count all subarrays having
// XOR of elements as given value K using Hash Map
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

// Function to find the count of subarrays of arr 
// with XOR value equals to k
int subarrayXor(vector<int>& arr, int k) {
    int res = 0; 

    // Create hash map that stores number of prefix arrays
    // corresponding to a XOR value
    unordered_map<int, int> mp;
  
  	int prefXOR = 0;
  
    for (int val : arr) {
      
        // Find XOR of current prefix
        prefXOR ^= val;

        // If prefXOR ^ k exist in mp then there is a subarray 
      	// ending at i with XOR equal to k
        res = res + mp[prefXOR ^ k];

        // If this prefix subarray has XOR equal to k
        if (prefXOR == k)
            res++;

        // Add the XOR of this subarray to the map
        mp[prefXOR]++;
    }

    // Return total count of subarrays having XOR of
    // elements as given value k
    return res;
}

int main() {
    vector<int> arr = { 4, 2, 2, 6, 4 };
    int k = 6;

    cout << subarrayXor(arr, k);
    return 0;
}
Java
// Java Program to count all subarrays having
// XOR of elements as given value K using Hash Map
import java.util.HashMap;

class GfG {
    // Function to find the count of subarrays of arr
    // with XOR value equals to k
    static int subarrayXor(int[] arr, int k) {
        int res = 0;

        // Create map that stores number of prefix arrays
        // corresponding to a XOR value
        HashMap<Integer, Integer> mp = new HashMap<>();

        int prefXOR = 0;

        for (int val : arr) {
          
            // Find XOR of current prefix
            prefXOR ^= val;

            // If prefXOR ^ k exists in mp then there is a subarray
            // ending at i with XOR equal to k
            res += mp.getOrDefault(prefXOR ^ k, 0);

            // If this prefix subarray has XOR equal to k
            if (prefXOR == k)
                res++;

            // Add the XOR of this subarray to the map
            mp.put(prefXOR, mp.getOrDefault(prefXOR, 0) + 1);
        }

        // Return total count of subarrays having XOR of
        // elements as given value k
        return res;
    }

    public static void main(String[] args) {
        int[] arr = { 4, 2, 2, 6, 4 };
        int k = 6;

        System.out.println(subarrayXor(arr, k));
    }
}
Python
# Python Program to count all subarrays having
# XOR of elements as given value K using Hash Map

def subarrayXor(arr, k):
    res = 0

    # Create map that stores number of prefix arrays
    # corresponding to a XOR value
    mp = {}

    prefXOR = 0

    for val in arr:
        # Find XOR of current prefix
        prefXOR ^= val

        # If prefXOR ^ k exists in mp then there is a subarray
        # ending at i with XOR equal to k
        res += mp.get(prefXOR ^ k, 0)

        # If this prefix subarray has XOR equal to k
        if prefXOR == k:
            res += 1

        # Add the XOR of this subarray to the map
        mp[prefXOR] = mp.get(prefXOR, 0) + 1

    # Return total count of subarrays having XOR of
    # elements as given value k
    return res

if __name__ == "__main__":
    arr = [4, 2, 2, 6, 4]
    k = 6

    print(subarrayXor(arr, k))
C#
// C# Program to count all subarrays having
// XOR of elements as given value K using Hash Map
using System;
using System.Collections.Generic;

class GfG {
    // Function to find the count of subarrays of arr
    // with XOR value equals to k
    static int subarrayXor(int[] arr, int k) {
        int res = 0;

        // Create map that stores number of prefix arrays
        // corresponding to a XOR value
        Dictionary<int, int> mp = new Dictionary<int, int>();

        int prefXOR = 0;

        // Calculate the answer
        foreach (int val in arr) {
            // Find XOR of current prefix
            prefXOR ^= val;

            // If prefXOR ^ k exists in mp then there is a subarray
            // ending at i with XOR equal to k
            if (mp.ContainsKey(prefXOR ^ k))
                res += mp[prefXOR ^ k];

            // If this prefix subarray has XOR equal to k
            if (prefXOR == k)
                res++;

            // Add the XOR of this subarray to the map
            if (!mp.ContainsKey(prefXOR))
                mp[prefXOR] = 0;
            mp[prefXOR]++;
        }

        // Return total count of subarrays having XOR of
        // elements as given value k
        return res;
    }

    static void Main(string[] args) {
        int[] arr = { 4, 2, 2, 6, 4 };
        int k = 6;

        Console.WriteLine(subarrayXor(arr, k));
    }
}
JavaScript
// JavaScript Program to count all subarrays having
// XOR of elements as given value K using Hash Map

function subarrayXor(arr, k) {
    let res = 0;

    // Create map that stores number of prefix arrays
    // corresponding to a XOR value
    const mp = new Map();

    let prefXOR = 0;

    // Calculate the answer
    for (const val of arr) {
        // Find XOR of current prefix
        prefXOR ^= val;

        // If prefXOR ^ k exists in mp then there is a subarray
        // ending at i with XOR equal to k
        res += mp.get(prefXOR ^ k) || 0;

        // If this prefix subarray has XOR equal to k
        if (prefXOR === k)
            res++;

        // Add the XOR of this subarray to the map
        mp.set(prefXOR, (mp.get(prefXOR) || 0) + 1);
    }

    // Return total count of subarrays having XOR of
    // elements as given value k
    return res;
}

// Driver Code
const arr = [4, 2, 2, 6, 4];
const k = 6;

console.log(subarrayXor(arr, k));

Output
4


Next Article
Practice Tags :

Similar Reads