Minimum steps to come back to starting point in a circular tour

Consider circular track with n points marked as 1, 2, …n. A person is initially placed on point k. The person moves m > 0, slot forward (in circular way) in each step. Find the minimum number of steps required so that the person reaches initial point k.

Examples:

Input : n = 9, k = 2, m = 6 
Output : 3
Explanation : Sequence of moves is
 2 => 8 => 5 => 2

Input : n = 6, k = 3, m = 2 
Output : 3



Naive Approach : Initialize a counter ‘i’ with ‘k’ and ‘count’ = 0. Further for each iteration increment ‘count’ add ‘m’ to ‘i’. Take its modulus with n i.e. i=((i+m)%n), if i > n. If i becomes equal to k then count will be our answer.

Time complexity: O(n).

 

Efficient Approach: We find GCD(n, m) and then divide n by GCD(n, m). That will be our answer. This can be explained as:
Think of n and m as per question now as we know that gcd(n, m) must divide n and the quotient tells us that after how many successive jumps(addition) of m numbers from starting position(say 0) we again reach the starting position.
Note: In circular arrangement of n numbers nth and 0th position are same.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum steps to reach
// starting position in a circular tour.
#include<bits / stdc++.h>
using namespace std;
  
// function for finding minimum steps
int minStroke(int n, int m)
{
    // return value n / gcd(n, m)
    return (n/__gcd(n, m));
}
  
// Driver function
int main()
{
    int n = 12, k = 5, m = 8;
    cout << minStroke(n, m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum steps to reach
// starting position in a circular tour.
  
class Test
{
    // method for finding minimum steps
    static int minStroke(int n, int m)
    {
        // return value n / gcd(n, m)
        return (n/gcd(n, m));
    }
      
    // method for gcd
    static int gcd(int n, int m) {
           
        if (n == 0 || m == 0)
           return 0;
        
          
        if (n == m)
            return n;
        
        if (n > m)
            return gcd(n-m, m);
        return gcd(n, m-n);
    }
  
    // Driver method
    public static void main(String args[])
    {
        int n = 12, k = 5, m = 8;
        System.out.println(minStroke(n, m));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find minimum 
# steps to reach starting position
# in a circular tour.
  
# function for finding minimum steps
def minStroke(n, m):
      
    # return value n / gcd(n, m)
    return (n / __gcd(n, m))
  
# method for gcd
def __gcd(n, m):
      
    if(n == 0 or m == 0):
        return 0
          
    if (n == m):
        return n
          
    if (n > m):
        return __gcd(n-m, m)
      
    return __gcd(n, m-n)
  
# Driver code
n = 12
k = 5
m = 8
  
print(minStroke(n, m))
  
# This code is contributed by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find minimum steps to reach
// starting position in a circular tour.
using System;
using System.Collections;
  
class GFG 
{
    // method for finding minimum steps
    static int minStroke(int n, int m)
    {
        // return value n / gcd(n, m)
        return (n/gcd(n, m));
    }
      
    // method for gcd
    static int gcd(int n, int m) {
          
        if (n == 0 || m == 0)
        return 0;
          
        if (n == m)
            return n;
          
        if (n > m)
            return gcd(n-m, m);
              
        return gcd(n, m-n);
    }
  
    // Driver method
    public static void Main()
    {
        int n = 12, m = 8;
        Console.WriteLine(minStroke(n, m));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum 
// steps to reach starting
// position in a circular tour.
  
// Recursive function to 
// return gcd of a and b
function __gcd($a, $b)
{
    // Everything divides 0
    if($a == 0 || $b == 0)
        return 0 ;
  
    // base case
    if($a == $b)
        return $a ;
      
    // a is greater
    if($a > $b)
        return __gcd($a - $b , $b);
  
    return __gcd($a , $b - $a);
}
  
// function for 
// finding minimum steps
function minStroke($n, $m)
{
    // return value n / gcd(n, m)
    return ($n / __gcd($n, $m));
}
  
// Driver Code
$n = 12; $k = 5; $m = 8;
echo minStroke($n, $m);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output:

3

Time Complexity: O(log(n))

This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007, vt_m



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.