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++
// 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 = 3 m = 3 a = [ 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 ?> |
Javascript
<script> // Javascript implementation of the approach // Function to count the steps required function findSteps(n, m, a) { // Start at 1 var cur = 1; // Initialize steps var steps = 0; for ( var 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 var n = 3, m = 3; var a = [ 2, 1, 2 ]; document.write( findSteps(n, m, a)); </script> |
Output
4
Complexity Analysis:
- Time Complexity: O(M), since the loop runs for M times.
- Auxiliary Space: O(1), since no extra space has been taken.
Please Login to comment...