Dashboard Temp Share Shortlinks Frames API

HTMLify

Count Subarrays with given XOR
Views: 2 | Author: prakhardoneria
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def subarrayXor(self, arr, k):
        res = 0
        pref_xor = 0
        counts = {0: 1}
        
        for val in arr:
            pref_xor ^= val
            
            target = pref_xor ^ k
            if target in counts:
                res += counts[target]
            
            counts[pref_xor] = counts.get(pref_xor, 0) + 1
            
        return res