human
Sorting array elements with set bits equal to K | Function to sort elements with set bits equal to k.
initialise two vectors.
first vector contains indices of required element.
second vector contains required elements.
sorting the elements in second vector.
replacing the elements with k set bits with the sorted elements.
printing the new sorted array elements.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to sorting array elements with set bits equal to K and the other steps required.
 def sortWithSetbits ( arr , n , k ) :
	v1 = [ ]
	v2 = [ ]
	for i in range ( 0 , n , 1 ) :
		if ( bin ( arr [ i ] ) . count ( '1' ) == k ) :
			v1 . append ( i )
			v2 . append ( arr [ i ] )
	v2 . sort ( reverse = False )
	for i in range ( 0 , len ( v1 ) , 1 ) :
		arr [ v1 [ i ] ] = v2 [ i ]
	for i in range ( 0 , n , 1 ) :
		print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 14 , 255 , 1 , 7 , 13 ]
	n = len ( arr )
	k = 3
	sortWithSetbits ( arr , n , k )