human
Queries for counts of array elements with values in given range | function to find first index >= x.
function to find last index <= x.
function to count elements within given range.
initialize result.
driver function.
Preprocess array.
Answer queries"
How can the above be solved in Python?
 Write me a code to queries for counts of array elements with values in given range and the other steps required?
assistant
#Here is code to queries for counts of array elements with values in given range and the other steps required.
 def lowerIndex ( arr , n , x ) :
	l = 0
	h = n - 1
	while ( l <= h ) :
		mid = int ( ( l + h ) / 2 )
		if ( arr [ mid ] >= x ) :
		h = mid - 1
		else :
		l = mid + 1
	return l
def upperIndex ( arr , n , x ) :
	l = 0
	h = n - 1
	while ( l <= h ) :
		mid = int ( ( l + h ) / 2 )
		if ( arr [ mid ] <= x ) :
		l = mid + 1
		else :
		h = mid - 1
	return h
def countInRange ( arr , n , x , y ) :
	count = 0 ;
	count = upperIndex ( arr , n , y ) - lowerIndex ( arr , n , x ) + 1 ;
	return count
arr = [ 1 , 3 , 4 , 9 , 10 , 3 ]
n = len ( arr )
arr . sort ( )
i = 1
j = 4
print ( countInRange ( arr , n , i , j ) )
i = 9
j = 12
print ( countInRange ( arr , n , i , j ) )