Steps required to visit M points in order on a circular ring of N points | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSteps ( int n , int m , int a [ ] ) { int cur = 1 ; int steps = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( a [ i ] >= cur ) steps += ( a [ i ] - cur ) ; else steps += ( n - cur + a [ i ] ) ; cur = a [ i ] ; } return steps ; } int main ( ) { int n = 3 , m = 3 ; int a [ ] = { 2 , 1 , 2 } ; cout << findSteps ( n , m , a ) ; }