Count of even set bits between XOR of two arrays | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countEvenBit ( int A [ ] , int B [ ] , int n , int m ) { int i , j , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { int x = __builtin_popcount ( A [ i ] ) ; if ( x & 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } int CountB [ m ] ; for ( i = 0 ; i < m ; i ++ ) { int x = __builtin_popcount ( B [ i ] ) ; if ( x & 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { cout << CountB [ i ] << ' ▁ ' ; } } int main ( ) { int A [ ] = { 4 , 2 , 15 , 9 , 8 , 8 } ; int B [ ] = { 3 , 4 , 22 } ; countEvenBit ( A , B , 6 , 3 ) ; return 0 ; }