Sum of bit differences among all pairs | Javascript program to compute sum of pairwise bit differences ; Initialize result ; Traverse over all bits ; count number of elements with i 'th bit set ; Add " count ▁ * ▁ ( n ▁ - ▁ count ) ▁ * ▁ 2" to the answer ; Driver code
function sumBitDifferences ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) if ( ( arr [ j ] & ( 1 << i ) ) ) count ++ ; ans += ( count * ( n - count ) * 2 ) ; } return ans ; } let arr = [ 1 , 3 , 5 ] ; let n = arr . length ; document . write ( sumBitDifferences ( arr , n ) ) ;