human
Check if all the set bits of the binary representation of N are at least K places away | Python3 program to check if all the set bits of the binary representation of N are at least K places away ..
Initialize check and count with 0.
The i - th bit is a set bit.
This is the first set bit so , start calculating all the distances between consecutive bits from here.
If count is less than K return false.
Adding the count as the number of zeroes increase between set bits.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to check if all the set bits of the binary representation of N are at least K places away and the other steps required.
 def CheckBits ( N , K ) :
	check = 0
	count = 0
	for i in range ( 31 , - 1 , - 1 ) :
		if ( ( 1 << i ) & N ) :
			if ( check == 0 ) :
				check = 1
			else :
				if ( count < K ) :
					return False
			count = 0
		else :
			count += 1
	return True
if __name__ == " _ _ main _ _ " :
	N = 5
	K = 1
	if ( CheckBits ( N , K ) ) :
		print ( " YES " )
	else :
		print ( " NO " )