Check if it is possible to reach (X, Y) from (1, 0) by given steps
Given two positive integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 0) by the given steps. In each step, possible moves from any cell (a, b) are (a, b + a) or (a + b, b). Print “Yes” if possible. Otherwise, print “No”.
Examples:
Input: X = 2, Y = 7
Output: Yes
Explanation: Sequence of moves to reach (2, 7) are: (1, 0) -> (1, 1) -> (2, 1) -> (2, 3) -> (2, 5) -> (2, 7).
Input: X = 30, Y = 24
Output: No
Naive Approach: The simplest approach is to try to move from points (X, Y) to (1, 0) recursively by using the operation (X – Y, Y) or (X, Y – X) until it becomes equals to (1, 0). If the X-coordinate becomes less than 1 or Y-coordinate becomes less than 0, then it is not possible to reach (1, 0). Therefore, print “No”. Otherwise, if (1, 0) is reached, print “Yes”.
Time Complexity: O(2log (min(X, Y)))
Auxiliary Space: O(1)
Efficient Approach: The idea is to observe the following properties:
- Try to solve the problem in reverse order i.e., it is possible to move from (X, Y) to (1, 0) by recursively moving to points (X – Y, Y) or (X, Y – X).
- The above property can be represented as follows:
GCD(X, Y) = GCD(X, Y – X) or GCD(X – Y, Y)
where,
Base Case is GCD(X, 0) = X
Now, notice that gcd of 1 and 0 i.e., gcd(1, 0) is 1.
Therefore, gcd of X and Y must also be 1 to reach (1, 0).
Therefore, from the above observations, the path from (1, 0) to (X, Y) always exists if GCD(X, Y) = 1. Print “Yes” if the GCD of the given two numbers is 1. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int GCD( int a, int b)
{
if (b == 0)
return a;
else
return GCD(b, a % b);
}
void check( int x, int y)
{
if (GCD(x, y) == 1) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int X = 2, Y = 7;
check(X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int GCD( int a,
int b)
{
if (b == 0 )
return a;
else
return GCD(b, a % b);
}
static void check( int x,
int y)
{
if (GCD(x, y) == 1 )
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
public static void main(String[] args)
{
int X = 2 , Y = 7 ;
check(X, Y);
}
}
|
Python3
def GCD(a, b):
if (b = = 0 ):
return a
else :
return GCD(b, a % b)
def check(x, y):
if (GCD(x, y) = = 1 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
X = 2
Y = 7
check(X, Y)
|
C#
using System;
class GFG{
static int GCD( int a, int b)
{
if (b == 0)
return a;
else
return GCD(b, a % b);
}
static void check( int x, int y)
{
if (GCD(x, y) == 1)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main()
{
int X = 2, Y = 7;
check(X, Y);
}
}
|
Javascript
<script>
function GCD(a, b)
{
if (b == 0)
return a;
else
return GCD(b, a % b);
}
function check(x, y)
{
if (GCD(x, y) == 1) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
let X = 2, Y = 7;
check(X, Y);
</script>
|
Time Complexity: O(log(min(X, Y))
Auxiliary Space: O(1)
Last Updated :
09 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...