human
Can you provide a code to count subarrays made up of elements having exactly K set bits and the other steps required given the following:
Count subarrays made up of elements having exactly K set bits | Function to count the number of set bits in an integer N.
Stores the count of set bits.
While N is non - zero.
If the LSB is 1 , then increment ans by 1.
Return the total set bits.
Function to count the number of subarrays having made up of elements having K set bits.
Stores the total count of resultant subarrays.
Traverse the given array.
If the current element has K set bits.
Otherwise.
Increment count of subarrays.
Return total count of subarrays.
Driver Code.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to count subarrays made up of elements having exactly K set bits and the other steps required.
 def countSet ( N ) :
	ans = 0
	while N :
		ans += N & 1
		N >>= 1
	return ans
def countSub ( arr , k ) :
	ans = 0
	setK = 0
	for i in arr :
		if countSet ( i ) == k :
			setK += 1
		else :
			setK = 0
		ans += setK
	return ans
arr = [ 4 , 2 , 1 , 5 , 6 ]
K = 2
print ( countSub ( arr , K ) )