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.
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.
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- In how many ways the ball will come back to the first boy after N turns
- Permutations of n things taken all at a time with m things never come together
- The Knight's tour problem | Backtracking-1
- Total nodes traversed in Euler Tour Tree
- Steps required to visit M points in order on a circular ring of N points
- Probability of reaching a point with 2 or 3 steps at a time
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
- Minimum number of moves to reach N starting from (1, 1)
- Minimum distance to visit given K points on X-axis after starting from the origin
- LSTM - Derivation of Back propagation through time
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Count minimum steps to get the given desired array
- Minimum steps needed to cover a sequence of points on an infinite grid
- Minimum steps to make the product of the array equal to 1
- Generate array with minimum sum which can be deleted in P steps
- Minimum steps to color the tree with given colors
- Find the minimum number of steps to reach M from N
- Minimum steps required to reduce all the elements of the array to zero