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:
- 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++
// 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); } |
Java
// 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 |
C#
// 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 |
Python3
# 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 = 3m = 3a = [2, 1, 2 ] print(findSteps(n, m, a)) # This code is contributed by ihritik |
PHP
<?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 ?> |
4
Recommended Posts:
- Minimum steps needed to cover a sequence of points on an infinite grid
- Prime points (Points that split a number into two primes)
- Ways to choose three points with distance between the most distant points <= L
- Number of Distinct Meeting Points on a Circular Road
- Number of Integral Points between Two Points
- Number of circular tours that visit all petrol pumps
- Number of quadrilaterals possible from the given points
- Minimum steps to come back to starting point in a circular tour
- Represent a given set of points by the best possible straight line
- Program to calculate distance between two points
- How to check if given four points form a square
- Number of triangles that can be formed with given N points
- Count maximum points on same line
- Maximum points of intersection n circles
- Check if the given 2-D points form T-shape or not
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



