human
Maximum sum by adding numbers with same number of set bits | count the number of bits for each element of array.
Count the number of set bits.
Function to return the the maximum sum.
Calculate the.
Assuming the number to be a maximum of 32 bits.
Add the number to the number of set bits.
Find the maximum sum.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to maximum sum by adding numbers with same number of set bits and the other steps required.
 def bit_count ( n ) :
	count = 0 ;
	while ( n > 0 ) :
		count += 1 ;
		n = n & ( n - 1 ) ;
	return count ;
def maxsum ( arr , n ) :
	bits = [ 0 ] * n ;
	for i in range ( n ) :
		bits [ i ] = bit_count ( arr [ i ] ) ;
	sum = [ 0 ] * 32 ;
	for i in range ( n ) :
		sum [ bits [ i ] ] += arr [ i ] ;
	maximum = 0 ;
	for i in range ( 32 ) :
		maximum = max ( sum [ i ] , maximum ) ;
	return maximum ;
arr = [ 2 , 3 , 8 , 5 , 6 , 7 ] ;
n = len ( arr ) ;
print ( maxsum ( arr , n ) ) ;