text: Count of even and odd set bit with array element after XOR with K | Function to get no of set bits in binary representation of positive integer n ; Function to store EVEN and odd variable ; Store the count of even and odd set bit ; Count the set bit using in built function ; Count of set - bit of K ; If y is odd then , count of even and odd set bit will be interchanged ; Else it will remain same as the original array ; Driver 's Code ; Function call to count even and odd
code: function __builtin_popcount ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function countEvenOdd ( arr , n , K ) { let even = 0 , odd = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let x = __builtin_popcount ( arr [ i ] ) ; if ( x % 2 == 0 ) even ++ ; else odd ++ ; } let y ; y = __builtin_popcount ( K ) ; if ( ( y & 1 ) != 0 ) { document . write ( " " + odd + " " + even ) ; } else { document . write ( " " + even + " " + odd ) ; } } let arr = [ 4 , 2 , 15 , 9 , 8 , 8 ] ; let K = 3 ; let n = arr . length ; countEvenOdd ( arr , n , K ) ;