Subarrays with XOR ‘K’

Problem Statement

No of Subarrays count with Xor = k

Intuition

Approach:

     xor
---------
4 2 2 4 6
     k
x _______
Take running xor

x^k = xor then xor ^ k = x we do this 

https://www.codingninjas.com/studio/problems/subarrays-with-xor-k_6826258?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&count=25&page=9&search=&sort_entity=order&sort_order=ASC&leftPanelTabValue=PROBLEM

https://www.youtube.com/watch?v=eZr-6p0B7ME&ab_channel=takeUforward

Approach 1:

C++
#include<bits/stdc++.h>

int subarraysWithSumK(vector < int > arr, int b) {
    int n = arr.size();
    int count = 0, x = 0;
    // xor, count;
    unordered_map<int,int> mp;

    for(int i=0; i<n; i++){
        x ^= arr[i];
        if(x == b)
            count++;

        if(mp.find(x^b) != mp.end()){
            count += mp[x^b];
        } 

        // if(mp.find(x^b) == mp.end()){
            mp[x]++;
        // }
    }

    return count;
}

Approach 2:

C++

Approach 3:

C++

Approach 4:

C++

Similar Problems

Last updated