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.
C++
#include <iostream>
using namespace std;
int minStroke( int n, int m, int k)
{
int i = k;
int count = 0;
do {
i = (i + m) % n;
count++;
} while (i != k);
return count;
}
int main()
{
int n = 12, k = 5, m = 8;
cout << minStroke(n, m, k);
return 0;
}
|
Java
public class Main
{
public static int minStroke( int n, int m, int k)
{
int i = k;
int count = 0 ;
do {
i = (i + m) % n;
count++;
} while (i != k);
return count;
}
public static void main(String[] args)
{
int n = 12 ;
int k = 5 ;
int m = 8 ;
System.out.print(minStroke(n, m, k));
}
}
|
Python3
def minStroke( n, m, k):
i = k;
count = 0 ;
i = (i + m) % n;
count + = 1 ;
while (i ! = k):
i = (i + m) % n;
count + = 1 ;
return count;
n = 12 ;
k = 5 ;
m = 8 ;
print (minStroke(n, m, k));
|
C#
using System;
public static class GFG
{
public static int minStroke( int n, int m, int k)
{
int i = k;
int count = 0;
do {
i = (i + m) % n;
count++;
} while (i != k);
return count;
}
public static void Main()
{
int n = 12;
int k = 5;
int m = 8;
Console.Write(minStroke(n, m, k));
}
}
|
Javascript
function minStroke(n, m, k)
{
let i = k;
let count = 0;
do {
i = (i + m) % n;
count++;
} while (i != k);
return count;
}
let n = 12, k = 5, m = 8;
console.log(minStroke(n, m, k));
|
Time complexity: O(n)
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int minStroke( int n, int m)
{
return (n / __gcd(n, m));
}
int main()
{
int n = 12, k = 5, m = 8;
cout << minStroke(n, m);
return 0;
}
|
Java
class Test {
static int minStroke( int n, int m)
{
return (n / gcd(n, m));
}
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);
}
public static void main(String args[])
{
int n = 12 , k = 5 , m = 8 ;
System.out.println(minStroke(n, m));
}
}
|
Python3
def minStroke(n, m):
return (n / __gcd(n, m))
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)
n = 12
k = 5
m = 8
print (minStroke(n, m))
|
C#
using System;
using System.Collections;
class GFG {
static int minStroke( int n, int m)
{
return (n / gcd(n, m));
}
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);
}
public static void Main()
{
int n = 12, m = 8;
Console.WriteLine(minStroke(n, m));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0 ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function minStroke( $n , $m )
{
return ( $n / __gcd( $n , $m ));
}
$n = 12; $k = 5; $m = 8;
echo minStroke( $n , $m );
?>
|
Javascript
<script>
function minStroke(n, m)
{
return (n/gcd(n, m));
}
function gcd(n, 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);
}
let n = 12, k = 5, m = 8;
document.write(minStroke(n, m));
</script>
|
Time Complexity: O(log(n))
Auxiliary space: O(1)
This article is contributed by Shivam Pradhan (anuj_charm). If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!