Count of even and odd set bit Array elements after XOR with K for Q queries | C # 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
using System ; 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 ) { Console . Write ( even + " ▁ " + odd + " STRNEWLINE " ) ; } else { Console . Write ( odd + " ▁ " + even + " STRNEWLINE " ) ; } } } public static void Main ( ) { 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 ) ; } }