Sum of array elements whose count of set bits are unique | Function to count the number of set bits in an integer N ; Stores the count of set bits ; Iterate until N is non - zero ; Stores the resultant count ; Function to calculate sum of all array elements whose count of set bits are unique ; Stores frequency of all possible count of set bits ; Stores the sum of array elements ; Traverse the array ; Count the number of set bits ; Traverse the array and Update the value of ans ; If frequency is 1 ; Driver Code
def setBitCount ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while n : NEW_LINE INDENT ans += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def getSum ( arr ) : NEW_LINE INDENT mp = { } NEW_LINE ans = 0 NEW_LINE for i in arr : NEW_LINE INDENT key = setBitCount ( i ) NEW_LINE mp [ key ] = [ 0 , i ] NEW_LINE DEDENT for i in arr : NEW_LINE INDENT key = setBitCount ( i ) NEW_LINE mp [ key ] [ 0 ] += 1 NEW_LINE DEDENT for i in mp : NEW_LINE INDENT if mp [ i ] [ 0 ] == 1 : NEW_LINE INDENT ans += mp [ i ] [ 1 ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 8 , 3 , 7 , 5 , 3 ] NEW_LINE getSum ( arr ) NEW_LINE