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
using System ; class GFG { static 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 ; } public static void Main ( ) { int n = 3 , m = 3 ; int [ ] a = { 2 , 1 , 2 } ; Console . WriteLine ( findSteps ( n , m , a ) ) ; } }