Traverse the deque and update the index of maximum element .
while ( len ( q ) and arr [ len ( q ) - 1 ] < arr [ i ] ) : NEW_LINE INDENT q . remove ( q [ len ( q ) - 1 ] ) NEW_LINE DEDENT q . append ( i ) NEW_LINE