Check if it is possible to reach (X, Y) from (1, 1) by given steps
Last Updated :
21 Jul, 2021
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++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to find the gcd of
// two numbers
int gcd(int a, int b)
{
// Base case
if (a < b)
{
int t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
// Recurse
return gcd(b, a % b);
}
// Function to print the answer
void printAnswer(int x, int y)
{
// GCD of X and Y
int val = gcd(x, y);
// If GCD is power of 2
if ((val & (val - 1)) == 0)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
// Given X and Y
int x = 4;
int y = 7;
// Function call
printAnswer(x, y);
return 0;
}
// This code is contributed by RohitOberoi
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the gcd of two numbers
public static int gcd(int a, int b)
{
// Base case
if (a < b) {
int t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
// Recurse
return gcd(b, a % b);
}
// Function to print the answer
static void printAnswer(int x, int y)
{
// GCD of X and Y
int val = gcd(x, y);
// If GCD is power of 2
if ((val & (val - 1)) == 0)
System.out.println("Yes");
else
System.out.println("No");
}
// Driver code
public static void main(String[] args)
{
// Given X and Y
int x = 4;
int y = 7;
// Function call
printAnswer(x, y);
}
}
Python3
# Python3 program for the
# above approach
# Function to find the gcd
# of two numbers
def gcd(a, b):
# Base case
if (a < b):
t = a
a = b
b = t
if (a % b == 0):
return b
# Recurse
return gcd(b, a % b)
# Function to print the
# answer
def printAnswer(x, y):
# GCD of X and Y
val = gcd(x, y)
# If GCD is power of 2
if ((val &
(val - 1)) == 0):
print("Yes")
else:
print("No")
# Driver code
if __name__ == "__main__":
# Given X and Y
x = 4
y = 7
# Function call
printAnswer(x, y)
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the gcd of two numbers
public static int gcd(int a, int b)
{
// Base case
if (a < b)
{
int t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
// Recurse
return gcd(b, a % b);
}
// Function to print the answer
static void printAnswer(int x, int y)
{
// GCD of X and Y
int val = gcd(x, y);
// If GCD is power of 2
if ((val & (val - 1)) == 0)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
// Driver code
public static void Main()
{
// Given X and Y
int x = 4;
int y = 7;
// Function call
printAnswer(x, y);
}
}
// This code is contributed by bgangwar59
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the gcd of
// two numbers
function gcd(a, b)
{
// Base case
if (a < b)
{
let t = a;
a = b;
b = t;
}
if (a % b == 0)
return b;
// Recurse
return gcd(b, a % b);
}
// Function to print the answer
function printAnswer(x, y)
{
// GCD of X and Y
let val = gcd(x, y);
// If GCD is power of 2
if ((val & (val - 1)) == 0)
document.write("Yes");
else
document.write("No");
}
// Driver code
// Given X and Y
let x = 4;
let y = 7;
// Function call
printAnswer(x, y);
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(Log(min(X, Y))) where (X, Y) is the given point.
Auxiliary Space: O(1)
Similar Reads
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 = 7Output: YesExplanat
5 min read
Check if it is possible to reach M from 0 by given paths Given an array arr[] consisting of N pairs of integers, where each pair (a, b) represents a path from a to b, the task is to check if it is possible to reach M from 0 using the given paths in the array arr[]. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {{0, 2
8 min read
Check if it is possible to move from (a, 0) to (b, 0) with given jumps Given two points, i.e. (a, 0) to (b, 0). The task is to check whether it is possible to move from (a,0) to (b,0) or not. One can move as (a, 0), (a+x, 0), (a+x+1, 0), (a, 2*x, 0), (a, 2*x+1, 0)...... Examples: Input: a = 3, x = 10, b = 4Output: No Input: a = 3, x = 2, b = 5Output: Yes Approach: An a
5 min read
Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps Given a point (X, Y) in a 2-D plane and an integer K, the task is to check whether it is possible to move from (0, 0) to the given point (X, Y) in exactly K moves. In a single move, the positions that are reachable from (X, Y) are (X, Y + 1), (X, Y - 1), (X + 1, Y) and (X - 1, Y).Examples: Input: X
4 min read
Find if it is possible to reach the end through given transitions Given, n points on X-axis and the list of allowed transition between the points. Find if it is possible to reach the end from starting point through these transitions only. Note: If there is a transition between points x1 and x2, then you can move from point x to any intermediate points between x1 a
5 min read