Count of even and odd set bit Array elements after XOR with K for Q queries | Javascript program to count number of even and odd set bits elements after XOR with a given element function even , odd ; ; 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
function keep_count ( arr , N ) { var count ; for ( i = 0 ; i < N ; i ++ ) { count = 0 ; while ( arr [ i ] != 0 ) { arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] ; count ++ ; } if ( count % 2 == 0 ) even ++ ; else odd ++ ; } return ; } function solveQueries ( arr , n , q , m ) { even = 0 ; odd = 0 ; keep_count ( arr , n ) ; for ( i = 0 ; i < m ; i ++ ) { var X = q [ i ] ; var count = 0 ; while ( X != 0 ) { X = ( X - 1 ) & X ; count ++ ; } if ( count % 2 == 0 ) { document . write ( even + " " + odd + " " ) ; } else { document . write ( odd + " " + even + " " ) ; } } } var arr = [ 2 , 7 , 4 , 5 , 3 ] ; var n = arr . length ; var q = [ 3 , 4 , 12 , 6 ] ; var m = q . length ; solveQueries ( arr , n , q , m ) ;