Given two integers X and Y, the task is to check if it is possible to reach (X, Y) from (1, 1) by the following possible moves:
- From a point (a, b) such that b > a, move to the to point (a, b – a).
- From a point (a, b) such that a > b, move to the to point (a – b, b).
- Move from any point (a, b) to point (2 * a, b) or (a, 2 * b).
If it is possible to reach (X, Y), print “Yes“. Otherwise, print “No”.
Examples:
Input: X = 4, Y = 7
Output: Yes
Explanation: Point (4, 7) can be reached by the following steps: (1, 1) -> (1, 2) -> (1, 4) -> (1, 8) -> (1, 7) -> (2, 7) -> (4, 7)
Input: X = 3, Y = 6
Output: No
Naive Approach: The simplest approach to solve the problem is to recursively check if (1, 1) can be reached from (X, Y) by making all possible moves from a point. If the point (1, 1) is reached at any point, print “Yes”. Otherwise, print “No”.
Time Complexity: O(N4) where N is the number of steps to reach the point (1, 1).
Auxiliary Space: O(1)
Efficient Approach: The idea is to find the greatest common divisor and observe the following facts:
- By moving the point from (a, b) to the points (a, b – a) or (a – b, b), there is no change in the GCD of the pair
- By moving the point from (a, b) to the points (2 * a, b) or (a, 2 * b), GCD may get doubled or it may remain the same.
- Therefore, from the above observations, point (1, 1) can move to point (X, Y) if and only if gcd of (X, Y) is a power of 2.
Follow the steps below to solve the above approach:
- Find the gcd of (X, Y) and store it in a variable, say val.
- Check if val is a power of 2 by checking if (val & amp;(val-1)) is equal to 0 where & is bitwise AND.
- If it is a power of two print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (a < b)
{
int t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
return gcd(b, a % b);
}
void printAnswer( int x, int y)
{
int val = gcd(x, y);
if ((val & (val - 1)) == 0)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int x = 4;
int y = 7;
printAnswer(x, y);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int gcd( int a, int b)
{
if (a < b) {
int t = a;
a = b;
b = t;
}
if (a % b == 0 )
return b;
return gcd(b, a % b);
}
static void printAnswer( int x, int y)
{
int val = gcd(x, y);
if ((val & (val - 1 )) == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int x = 4 ;
int y = 7 ;
printAnswer(x, y);
}
}
|
Python3
def gcd(a, b):
if (a < b):
t = a
a = b
b = t
if (a % b = = 0 ):
return b
return gcd(b, a % b)
def printAnswer(x, y):
val = gcd(x, y)
if ((val &
(val - 1 )) = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
x = 4
y = 7
printAnswer(x, y)
|
C#
using System;
class GFG{
public static int gcd( int a, int b)
{
if (a < b)
{
int t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
return gcd(b, a % b);
}
static void printAnswer( int x, int y)
{
int val = gcd(x, y);
if ((val & (val - 1)) == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main()
{
int x = 4;
int y = 7;
printAnswer(x, y);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a < b)
{
let t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
return gcd(b, a % b);
}
function printAnswer(x, y)
{
let val = gcd(x, y);
if ((val & (val - 1)) == 0)
document.write( "Yes" );
else
document.write( "No" );
}
let x = 4;
let y = 7;
printAnswer(x, y);
</script>
|
Time Complexity: O(Log(min(X, Y))) where (X, Y) is the given point.
Auxiliary Space: O(1)