Steps required to visit M points in order on a circular ring of N points

Given an integer ‘n’, consider a circular ring containing ‘n’ points numbered from ‘1’ to ‘n’ such that you can move in the following way :

1 -> 2 -> 3 -> ….. -> n -> 1 -> 2 -> 3 -> ……

Also, given an array of integers (of size ‘m’), the task is to find the number of steps it’ll take to get to every point in the array in order starting at ‘1’

Examples :

Input: n = 3, m = 3, arr[] = {2, 1, 2}
Output: 4
The sequence followed is 1->2->3->1->2

Input: n = 2, m = 1, arr[] = {2}
Output: 1
The sequence followed is 1->2

Approach:Let’s denote the current position by cur and next position by nxt. This gives us 2 cases:

  1. If cur is smaller than nxt, you can move to it in nxt – cur steps.
  2. Otherwise, you first need to reach the point n in n – cur steps and then you can move to nxt in cur steps.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the steps required
int findSteps(int n, int m, int a[])
{
  
    // Start at 1
    int cur = 1;
  
    // Initialize steps
    int steps = 0;
    for (int i = 0; i < m; i++) {
  
        // If nxt is greater than cur
        if (a[i] >= cur)
            steps += (a[i] - cur);
        else
            steps += (n - cur + a[i]);
  
        // Now we are at a[i]
        cur = a[i];
    }
    return steps;
}
  
// Driver code
int main()
{
    int n = 3, m = 3;
    int a[] = { 2, 1, 2 };
    cout << findSteps(n, m, a);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to count the steps required
static int findSteps(int n, int m, 
                     int a[])
{
  
    // Start at 1
    int cur = 1;
  
    // Initialize steps
    int steps = 0;
    for (int i = 0; i < m; i++)
    {
  
        // If nxt is greater than cur
        if (a[i] >= cur)
            steps += (a[i] - cur);
        else
            steps += (n - cur + a[i]);
  
        // Now we are at a[i]
        cur = a[i];
    }
    return steps;
}
  
// Driver code
public static void main(String []args)
{
    int n = 3, m = 3;
    int a[] = { 2, 1, 2 };
    System.out.println(findSteps(n, m, a));
}
}
  
// This code is contributed by ihritik

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
// Function to count the 
// steps required
static int findSteps(int n, 
                     int m, int []a)
{
  
    // Start at 1
    int cur = 1;
  
    // Initialize steps
    int steps = 0;
    for (int i = 0; i < m; i++) 
    {
  
        // If nxt is greater than cur
        if (a[i] >= cur)
            steps += (a[i] - cur);
        else
            steps += (n - cur + a[i]);
  
        // Now we are at a[i]
        cur = a[i];
    }
    return steps;
}
  
// Driver code
public static void Main()
{
    int n = 3, m = 3;
    int []a = { 2, 1, 2 };
    Console.WriteLine(findSteps(n, m, a));
}
}
  
// This code is contributed by ihritik

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to count the steps required
def findSteps(n, m, a):
  
    # Start at 1
    cur = 1
  
    # Initialize steps
    steps = 0
    for i in range(0, m): 
  
        # If nxt is greater than cur
        if (a[i] >= cur):
            steps += (a[i] - cur)
        else:
            steps += (n - cur + a[i])
  
        # Now we are at a[i]
        cur = a[i]
      
    return steps
  
# Driver code
n = 3
m = 3
a = [2, 1, 2 ]
print(findSteps(n, m, a))
  
# This code is contributed by ihritik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to count the steps required
function findSteps($n, $m, $a)
{
  
    // Start at 1
    $cur = 1;
  
    // Initialize steps
    $steps = 0;
    for ($i = 0; $i < $m; $i++) 
    {
  
        // If nxt is greater than cur
        if ($a[$i] >= $cur)
            $steps += ($a[$i] - $cur);
        else
            $steps += ($n - $cur + $a[$i]);
  
        // Now we are at a[i]
        $cur = $a[$i];
    }
    return $steps;
}
  
// Driver code
$n = 3;
$m = 3;
$a = array(2, 1, 2 );
echo findSteps($n, $m, $a);
  
// This code is contributed by ihritik
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik