human
Queries to count array elements from a given range having a single set bit | Function to check whether only one bit is set or not.
Function to perform Range - query.
Function to count array elements with a single set bit for each range in a query.
Initialize array for Prefix sum.
Driver Code.
Given array.
Size of the array.
Given queries.
Size of queries array"
How can the above be solved in Python?
assistant
#Here is code to queries to count array elements from a given range having a single set bit and the other steps required.
 def check ( x ) :
	if ( ( ( x ) & ( x - 1 ) ) == 0 ) :
		return 1
	return 0
def query ( l , r , pre ) :
	if ( l == 0 ) :
		return pre [ r ]
	else :
		return pre [ r ] - pre [ l - 1 ]
def countInRange ( arr , N , queries , Q ) :
	pre = [ 0 ] * N
	pre [ 0 ] = check ( arr [ 0 ] )
	for i in range ( 1 , N ) :
		pre [ i ] = pre [ i - 1 ] + check ( arr [ i ] )
	c = 0
	while ( Q > 0 ) :
		l = queries [ 0 ]
		r = queries [ 1 ]
		c += 1
		print ( query ( l , r , pre ) , end = " ▁ " )
		Q -= 1
if __name__ == " _ _ main _ _ " :
	arr = [ 12 , 11 , 16 , 8 , 2 , 5 , 1 , 3 , 256 , 1 ]
	N = len ( arr )
	queries = [ [ 0 , 9 ] , [ 4 , 9 ] ]
	Q = len ( queries )
	countInRange ( arr , N , queries , Q )