Given two integers X and Y, the task is to count the minimum number of moves required to reach the point (1, 1), starting from the point (1, 1), if the allowed moves from any point, say (a, b) are either (a – b, b) or (a, b – a).
Examples:
Input: X = 3, Y = 1
Output: 2
Explanation: Required sequence of moves are (3, 1) → (2, 1) → (1, 1)
Input: X = 2, Y = 2
Output: -1
Naive Approach: The simplest approach to solve the problem is to keep subtracting the smaller number from the greater number, until they become equal. If at any instant, either X or Y becomes less than 1, print -1. Otherwise, print the number of subtractions performed as the minimum number of operations required.
Time Complexity: O(max(X, Y))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to replace the repetitive subtractions by modulo operation, which is similar to calculating GCD using the Euclidean algorithm. Follow the steps below to solve the problem:
- Initialize a variable, say cnt with 0, for storing the minimum number of steps.
- Iterate while X and Y are both non-zero and perform the following operations:
- If the value of X > Y, then add X/Y to cnt. Update X to X%Y
- If the value of Y > X, then add Y/X to cnt. Update Y to Y%X
- Check if one of them is greater than 1, then print -1.
- Otherwise, decrease the value of cnt by 1, because one extra step is taken to make one of them equal to 0.
- Print the value of cnt as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumSteps( int x, int y)
{
int cnt = 0;
while (x != 0 && y != 0) {
if (x > y) {
cnt += x / y;
x %= y;
}
else {
cnt += y / x;
y %= x;
}
}
cnt--;
if (x > 1 || y > 1)
cnt = -1;
cout << cnt;
}
int main()
{
int x = 3, y = 1;
minimumSteps(x, y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minimumSteps( int x, int y)
{
int cnt = 0 ;
while (x != 0 && y != 0 )
{
if (x > y)
{
cnt += x / y;
x %= y;
}
else
{
cnt += y / x;
y %= x;
}
}
cnt--;
if (x > 1 || y > 1 )
cnt = - 1 ;
System.out.println(cnt);
}
public static void main (String[] args)
{
int x = 3 , y = 1 ;
minimumSteps(x, y);
}
}
|
Python3
def minimumSteps(x, y):
cnt = 0
while (x ! = 0 and y ! = 0 ):
if (x > y):
cnt + = x / y
x % = y
else :
cnt + = y / x
y % = x
cnt - = 1
if (x > 1 or y > 1 ):
cnt = - 1
print ( int (cnt))
if __name__ = = '__main__' :
x = 3
y = 1
minimumSteps(x, y)
|
C#
using System;
class GFG{
public static void minimumSteps( int x, int y)
{
int cnt = 0;
while (x != 0 && y != 0)
{
if (x > y)
{
cnt += x / y;
x %= y;
}
else
{
cnt += y / x;
y %= x;
}
}
cnt--;
if (x > 1 || y > 1)
cnt = -1;
Console.WriteLine(cnt);
}
public static void Main()
{
int x = 3, y = 1;
minimumSteps(x, y);
}
}
|
Javascript
<script>
function minimumSteps(x, y)
{
var cnt = 0;
while (x != 0 && y != 0) {
if (x > y) {
cnt += x / y;
x %= y;
}
else {
cnt += y / x;
y %= x;
}
}
cnt--;
if (x > 1 || y > 1)
cnt = -1;
document.write( cnt);
}
var x = 3, y = 1;
minimumSteps(x, y);
</script>
|
Time Complexity: O(log(max(X, Y)))
Auxiliary Space: O(1)
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 geeks!
Last Updated :
11 May, 2021
Like Article
Save Article