Find number of subarrays with XOR value a power of 2 | Python3 Program to count number of subarrays with Bitwise - XOR as power of 2 ; Function to find number of subarrays ; Hash Map to store prefix XOR values ; When no element is selected ; Check for all the powers of 2 , till a MAX value ; Insert Current prefixxor in Hash Map ; Driver Code
MAX = 10 NEW_LINE def findSubarray ( array , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE mp [ 0 ] = 1 NEW_LINE answer = 0 NEW_LINE preXor = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT value = 1 NEW_LINE preXor ^= array [ i ] NEW_LINE for j in range ( 1 , MAX + 1 ) : NEW_LINE INDENT Y = value ^ preXor NEW_LINE if ( Y in mp . keys ( ) ) : NEW_LINE INDENT answer += mp [ Y ] NEW_LINE DEDENT value *= 2 NEW_LINE DEDENT if ( preXor in mp . keys ( ) ) : NEW_LINE INDENT mp [ preXor ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ preXor ] = 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT array = [ 2 , 6 , 7 , 5 , 8 ] NEW_LINE n = len ( array ) NEW_LINE print ( findSubarray ( array , n ) ) NEW_LINE