Store the frequency of respective counts of set bits
map < int , int > setbits ; for ( int i = 0 ; i < N ; i ++ ) { setbits [ bitscount [ arr [ i ] ] ] ++ ; } int ans = 0 ; for ( auto it : setbits ) { ans += it . second * ( it . second - 1 ) / 2 ; } return ans ; }