Length of the longest subarray whose Bitwise XOR is K | C # program to implement the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int longestLenXORK ( int [ ] arr , int N , int K ) { int prefixXOR = 0 ; int maxLen = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; mp . Add ( 0 , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { prefixXOR ^= arr [ i ] ; if ( mp . ContainsKey ( prefixXOR ^ K ) ) { maxLen = Math . Max ( maxLen , ( i - mp [ prefixXOR ^ K ] ) ) ; } if ( ! mp . ContainsKey ( prefixXOR ) ) { mp . Add ( prefixXOR , i ) ; } } return maxLen ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 7 , 2 } ; int N = arr . Length ; int K = 1 ; Console . Write ( longestLenXORK ( arr , N , K ) ) ; } }