Find number of subarrays with XOR value a power of 2 | Javascript 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
var MAX = 10 ; function findSubarray ( array , n ) { var mp = new Map ( ) ; mp . set ( 0 , 1 ) ; var answer = 0 ; var preXor = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var value = 1 ; preXor ^= array [ i ] ; for ( var j = 1 ; j <= MAX ; j ++ ) { var Y = value ^ preXor ; if ( mp . has ( Y ) ) { answer += mp . get ( Y ) ; } value *= 2 ; } if ( mp . has ( preXor ) ) { mp . set ( preXor , mp . get ( preXor ) + 1 ) ; } else { mp . set ( preXor , 1 ) ; } } return answer ; } var array = [ 2 , 6 , 7 , 5 , 8 ] ; var n = array . length ; document . write ( findSubarray ( array , n ) ) ;