human
Number of indexes with equal elements in given range | Python program to count the number of indexes in range L R such that Ai = Ai + 1.
array to store count of index from 0 to i that obey condition.
precomputing prefixans [ ] array.
traverse to compute the prefixans [ ] array.
def that answers every query in O ( 1 ).
Driver Code.
pre - computation.
1 - st query.
2 nd query"
How can the above be solved in Python?
assistant
#Here is code to number of indexes with equal elements in given range and the other steps required.
 N = 1000
prefixans = [ 0 ] * N ;
def countIndex ( a , n ) :
	global N , prefixans
	for i in range ( 0 , n - 1 ) :
		if ( a [ i ] == a [ i + 1 ] ) :
			prefixans [ i ] = 1
		if ( i != 0 ) :
			prefixans [ i ] = ( prefixans [ i ] + prefixans [ i - 1 ] )
def answer_query ( l , r ) :
	global N , prefixans
	if ( l == 0 ) :
		return prefixans [ r - 1 ]
	else :
		return ( prefixans [ r - 1 ] - prefixans [ l - 1 ] )
a = [ 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ]
n = len ( a )
countIndex ( a , n )
L = 1
R = 8
print ( answer_query ( L , R ) )
L = 0
R = 4
print ( answer_query ( L , R ) )