Count of subarrays of size K with elements having even frequencies | Function to return count of required subarrays ; If K is odd ; Not possible to have any such subarrays ; Stores the starting index of every subarrays ; Stores the count of required subarrays ; Stores Xor of the current subarray . ; Xor of first subarray of size K ; If all elements appear even number of times , increase the count of such subarrays ; Remove the starting element from the current subarray ; Traverse the array for the remaining subarrays ; Update Xor by adding the last element of the current subarray ; Increment i ; If currXor becomes 0 , then increment count ; Update currXor by removing the starting element of the current subarray ; Return count ; Driver Code
function countSubarray ( arr , K , N ) { if ( K % 2 != 0 ) return 0 ; if ( N < K ) return 0 ; var start = 0 ; var i = 0 ; var count = 0 ; var currXor = arr [ i ] ; i ++ ; while ( i < K ) { currXor ^= arr [ i ] ; i ++ ; } if ( currXor == 0 ) count ++ ; currXor ^= arr [ start ] ; start ++ ; while ( i < N ) { currXor ^= arr [ i ] ; i ++ ; if ( currXor == 0 ) count ++ ; currXor ^= arr [ start ] ; start ++ ; } return count ; } var arr = [ 2 , 4 , 4 , 2 , 2 , 4 ] ; var K = 4 ; var N = arr . length ; document . write ( countSubarray ( arr , K , N ) ) ;