Product of count of set bits present in binary representations of elements in an array | 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
function countbits ( n ) { let count = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) count ++ ; n = Math . floor ( n / 2 ) ; } return count ; } function BitProduct ( arr , N ) { let product = 1 ; for ( let i = 0 ; i < N ; i ++ ) { let bits = countbits ( arr [ i ] ) ; product *= bits ; } return product ; } let arr = [ 3 , 2 , 4 , 1 , 5 ] ; let N = arr . length ; document . write ( BitProduct ( arr , N ) ) ;