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
def countSubarray ( arr , K , N ) : NEW_LINE INDENT if ( K % 2 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( N < K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT start = 0 NEW_LINE i = 0 NEW_LINE count = 0 NEW_LINE currXor = arr [ i ] NEW_LINE i += 1 NEW_LINE while ( i < K ) : NEW_LINE INDENT currXor ^= arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT if ( currXor == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT currXor ^= arr [ start ] NEW_LINE start += 1 NEW_LINE while ( i < N ) : NEW_LINE INDENT currXor ^= arr [ i ] NEW_LINE i += 1 NEW_LINE if ( currXor == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT currXor ^= arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 4 , 2 , 2 , 4 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( countSubarray ( arr , K , N ) ) NEW_LINE DEDENT