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
def LongestLenXORK ( arr , N , K ) : NEW_LINE INDENT prefixXOR = 0 NEW_LINE maxLen = 0 NEW_LINE mp = { } NEW_LINE mp [ 0 ] = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT prefixXOR ^= arr [ i ] NEW_LINE if ( prefixXOR ^ K ) in mp : NEW_LINE INDENT maxLen = max ( maxLen , ( i - mp [ prefixXOR ^ K ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT mp [ prefixXOR ] = i NEW_LINE DEDENT DEDENT return maxLen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 7 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE print ( LongestLenXORK ( arr , N , K ) ) NEW_LINE DEDENT