Length of the longest subarray whose Bitwise XOR is K | Function to find the length of the longest subarray whose bitwise XOR is equal to K ; Stores prefix XOR of the array ; Stores length of longest subarray having bitwise XOR equal to K ; Stores index of prefix XOR of the array ; Insert 0 into the map ; Traverse the array ; Update prefixXOR ; If ( prefixXOR ^ K ) present in the map ; Update maxLen ; If prefixXOR not present in the Map ; Insert prefixXOR into the map ; Driver Code
function LongestLenXORK ( arr , N , K ) { var prefixXOR = 0 ; var maxLen = 0 ; var mp = new Map ( ) ; mp . set ( 0 , - 1 ) ; for ( var i = 0 ; i < N ; i ++ ) { prefixXOR ^= arr [ i ] ; if ( mp . has ( prefixXOR ^ K ) ) { maxLen = Math . max ( maxLen , ( i - mp . get ( prefixXOR ^ K ) ) ) ; } if ( ! mp . has ( prefixXOR ) ) { mp . set ( prefixXOR , i ) ; } } return maxLen ; } var arr = [ 1 , 2 , 4 , 7 , 2 ] ; var N = arr . length ; var K = 1 ; document . write ( LongestLenXORK ( arr , N , K ) ) ;