Product of count of set bits present in binary representations of elements in an array | C ++ program for the above approach ; Function to count the set bits in an integer ; Stores the count of set bits ; Iterate while N is not equal to 0 ; Increment count by 1 ; Divide N by 2 ; Return the total count obtained ; Function to find the product of count of set bits present in each element of an array ; Stores the resultant product ; Traverse the array arr [ ] ; Stores the count of set bits of arr [ i ] ; Update the product ; Return the resultant product ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countbits ( int n ) { int count = 0 ; while ( n != 0 ) { if ( n & 1 ) count ++ ; n = n / 2 ; } return count ; } int BitProduct ( int arr [ ] , int N ) { int product = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int bits = countbits ( arr [ i ] ) ; product *= bits ; } return product ; } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 1 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << BitProduct ( arr , N ) ; return 0 ; }