Number of indexes with equal elements in given range | function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query
def answer_query ( a , n , l , r ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r ) : NEW_LINE INDENT if ( a [ i ] == a [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT a = [ 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ] NEW_LINE n = len ( a ) NEW_LINE L = 1 NEW_LINE R = 8 NEW_LINE print ( answer_query ( a , n , L , R ) ) NEW_LINE L = 0 NEW_LINE R = 4 NEW_LINE print ( answer_query ( a , n , L , R ) ) NEW_LINE