Steps required to visit M points in order on a circular ring of N points | Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code
def findSteps ( n , m , a ) : NEW_LINE INDENT cur = 1 NEW_LINE steps = 0 NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( a [ i ] >= cur ) : NEW_LINE INDENT steps += ( a [ i ] - cur ) NEW_LINE DEDENT else : NEW_LINE INDENT steps += ( n - cur + a [ i ] ) NEW_LINE DEDENT cur = a [ i ] NEW_LINE DEDENT return steps NEW_LINE DEDENT n = 3 NEW_LINE m = 3 NEW_LINE a = [ 2 , 1 , 2 ] NEW_LINE print ( findSteps ( n , m , a ) ) NEW_LINE