Count of even and odd set bit Array elements after XOR with K for Q queries | Java 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
class GFG { static int even , odd ; static void keep_count ( int arr [ ] , int N ) { int count ; for ( int 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 ; } static void solveQueries ( int arr [ ] , int n , int q [ ] , int m ) { even = 0 ; odd = 0 ; keep_count ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int X = q [ i ] ; int count = 0 ; while ( X != 0 ) { X = ( X - 1 ) & X ; count ++ ; } if ( count % 2 == 0 ) { System . out . print ( even + " ▁ " + odd + "NEW_LINE"); } else { System . out . print ( odd + " ▁ " + even + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 4 , 5 , 3 } ; int n = arr . length ; int q [ ] = { 3 , 4 , 12 , 6 } ; int m = q . length ; solveQueries ( arr , n , q , m ) ; } }