Open In App

Minimum number of given moves required to reach (1, 1) from (X, Y)

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
int minimumSteps(int x, int y)
{
    // Store the required result
    int cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0) {
 
        // If x is greater than y
        if (x > y) {
 
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else {
 
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    cout << cnt;
}
 
// Driver Code
int main()
{
    // Given X and Y
    int x = 3, y = 1;
    minimumSteps(x, y);
 
    return 0;
}


Java




// Java program for above approach
import java.util.*;
 
class GFG{
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
static void minimumSteps(int x, int y)
{
     
    // Store the required result
    int cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0)
    {
         
        // If x is greater than y
        if (x > y)
        {
             
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else
        {
             
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    System.out.println(cnt);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given X and Y
    int x = 3, y = 1;
     
    minimumSteps(x, y);
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python3 program for the above approach
 
# Function to count the number of steps
# required to convert (x, y) to (1, 1)
def minimumSteps(x, y):
     
    # Store the required result
    cnt = 0
 
    # Iterate while both x
    # and y are not equal to 0
    while (x != 0 and y != 0):
         
        # If x is greater than y
        if (x > y):
             
            # Update count and value of x
            cnt += x / y
            x %= y
 
        # Otherwise
        else:
             
            # Update count and value of y
            cnt += y / x
            y %= x
             
    cnt -= 1
 
    # If both x and y > 1
    if (x > 1 or y > 1):
        cnt = -1
 
    # Print the result
    print(int(cnt))
 
# Driver Code
if __name__ == '__main__':
     
    # Given X and Y
    x = 3
    y = 1
     
    minimumSteps(x, y)
     
# This code is contributed by ipg2016107


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
public static void minimumSteps(int x, int y)
{
     
    // Store the required result
    int cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0)
    {
         
        // If x is greater than y
        if (x > y)
        {
             
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else
        {
             
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    Console.WriteLine(cnt);
}
 
// Driver Code
public static void Main()
{
     
    // Given X and Y
    int x = 3, y = 1;
     
    minimumSteps(x, y);
}
}
 
// This code is contributed by mohit kumar 29


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to count the number of steps
// required to convert (x, y) to (1, 1)
function minimumSteps(x, y)
{
    // Store the required result
    var cnt = 0;
 
    // Iterate while both x
    // and y are not equal to 0
    while (x != 0 && y != 0) {
 
        // If x is greater than y
        if (x > y) {
 
            // Update count and value of x
            cnt += x / y;
            x %= y;
        }
 
        // Otherwise
        else {
 
            // Update count and value of y
            cnt += y / x;
            y %= x;
        }
    }
    cnt--;
 
    // If both x and y > 1
    if (x > 1 || y > 1)
        cnt = -1;
 
    // Print the result
    document.write( cnt);
}
 
// Driver Code
// Given X and Y
var x = 3, y = 1;
minimumSteps(x, y);
 
</script>


Output: 

2

 

Time Complexity: O(log(max(X, Y)))
Auxiliary Space: O(1)

 



Last Updated : 11 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads