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 the next position by nxt.
This gives us 2 cases:
- If cur is smaller than nxt, you can move to it in nxt – cur steps.
- 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++
#include <bits/stdc++.h>
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);
}
|
Java
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(String []args)
{
int n = 3 , m = 3 ;
int a[] = { 2 , 1 , 2 };
System.out.println(findSteps(n, m, a));
}
}
|
C#
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));
}
}
|
Python3
def findSteps(n, m, a):
cur = 1
steps = 0
for i in range ( 0 , m):
if (a[i] > = cur):
steps + = (a[i] - cur)
else :
steps + = (n - cur + a[i])
cur = a[i]
return steps
n = 3
m = 3
a = [ 2 , 1 , 2 ]
print (findSteps(n, m, a))
|
PHP
<?php
function findSteps( $n , $m , $a )
{
$cur = 1;
$steps = 0;
for ( $i = 0; $i < $m ; $i ++)
{
if ( $a [ $i ] >= $cur )
$steps += ( $a [ $i ] - $cur );
else
$steps += ( $n - $cur + $a [ $i ]);
$cur = $a [ $i ];
}
return $steps ;
}
$n = 3;
$m = 3;
$a = array (2, 1, 2 );
echo findSteps( $n , $m , $a );
?>
|
Javascript
<script>
function findSteps(n, m, a)
{
var cur = 1;
var steps = 0;
for ( var i = 0; i < m; i++) {
if (a[i] >= cur)
steps += (a[i] - cur);
else
steps += (n - cur + a[i]);
cur = a[i];
}
return steps;
}
var n = 3, m = 3;
var a = [ 2, 1, 2 ];
document.write( findSteps(n, m, a));
</script>
|
Complexity Analysis:
- Time Complexity: O(M), since the loop runs for M times.
- Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
07 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...