Count of even set bits between XOR of two arrays | Function that count the XOR of B [ ] with all the element in A [ ] having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for B [ ] such that XOR with all the element in A [ ] having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code
function countEvenBit ( A , B , n , m ) { let i , j , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { let x = bitCount ( A [ i ] ) ; if ( x & 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } let CountB = new Array ( m ) ; for ( i = 0 ; i < m ; i ++ ) { let x = bitCount ( B [ i ] ) ; if ( x & 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { document . write ( CountB [ i ] + " " ) ; } } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let A = [ 4 , 2 , 15 , 9 , 8 , 8 ] ; let B = [ 3 , 4 , 22 ] ; countEvenBit ( A , B , 6 , 3 ) ;