Count of even and odd set bit Array elements after XOR with K for Q queries | Python3 program to count number of even and odd set bits elements after XOR with a given element ; Store the count of set bits ; Brian Kernighan 's algorithm ; Function to solve Q queries ; Store set bits in X ; Count set bits of X ; Driver code
even = 0 NEW_LINE odd = 0 NEW_LINE def keep_count ( arr , N ) : NEW_LINE INDENT global even NEW_LINE global odd NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( arr [ i ] != 0 ) : NEW_LINE INDENT arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT def solveQueries ( arr , n , q , m ) : NEW_LINE INDENT global even NEW_LINE global odd NEW_LINE keep_count ( arr , n ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT X = q [ i ] NEW_LINE count = 0 NEW_LINE while ( X != 0 ) : NEW_LINE INDENT X = ( X - 1 ) & X NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT print ( even , odd ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( odd , even ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 7 , 4 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE q = [ 3 , 4 , 12 , 6 ] NEW_LINE m = len ( q ) NEW_LINE solveQueries ( arr , n , q , m ) NEW_LINE DEDENT