Check if a destination is reachable from source with two movements allowed | Set 2
Given a pair of coordinates (X1, Y1)(source) and (X2, Y2)(destination), the task is to check if it is possible to reach the destination form the source by the following movements from any cell (X, Y):
- (X + Y, Y)
- (X, Y + X)
Note: All coordinates are positive and can be as large as 1018.
Examples:
Input: X1 = 2, Y1 = 10, X2 = 26, Y2 = 12
Output: Yes
Explanation: Possible path: (2, 10) ? (2, 12) ? (14, 12) ? (26, 12)
Therefore, a path exists between source and destination.
Input: X1 = 20, Y1 = 10, X2 = 6, Y2 = 12
Output: No
Naive Approach: The simplest approach to solve the problem is by using recursion. Refer to the article check if a destination is reachable from source with two movements allowed for the recursive approach.
Efficient Approach: The main idea is to check if a path from the destination coordinates (X2, Y2) to the source (X1, Y1) exists or not.
Follow the steps below to solve the problem:
- Keep subtracting the smallest of (X2, Y2) from the largest of (X2, Y2) and stop if X2 becomes less than X1 or Y2 becomes less than Y1.
- Now, compare (X1, Y1) and modified (X2, Y2). If X1 is equal to X2 and Y1 is equal to Y2, then print “Yes“.
- If X1 is not equal to X2 or Y1 is equal, not Y2, then print “No“.
To optimize the complexity of the subtraction operation, the modulus operation can be used instead. Simply perform x2 = x2 % y2 and y2 = y2 % x2 and check for the necessary condition mentioned above.
C++
#include <bits/stdc++.h>
using namespace std;
bool isReachable( long long x1, long long y1,
long long x2, long long y2)
{
while (x2 > x1 && y2 > y1) {
if (x2 > y2)
x2 %= y2;
else
y2 %= x2;
}
if (x2 == x1)
return (y2 - y1) >= 0
&& (y2 - y1) % x1 == 0;
else if (y2 == y1)
return (x2 - x1) >= 0
&& (x2 - x1) % y1 == 0;
else
return 0;
}
int main()
{
long long source_x = 2, source_y = 10;
long long dest_x = 26, dest_y = 12;
if (isReachable(source_x, source_y,
dest_x, dest_y))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Python3
def isReachable(x1, y1, x2, y2):
while (x2 > x1 and y2 > y1):
if (x2 > y2):
x2 % = y2
else :
y2 % = x2
if (x2 = = x1):
return (y2 - y1) > = 0 and (
y2 - y1) % x1 = = 0
elif (y2 = = y1):
return (x2 - x1) > = 0 and (
x2 - x1) % y1 = = 0
else :
return 0
source_x = 2
source_y = 10
dest_x = 26
dest_y = 12
if (isReachable(source_x, source_y,
dest_x, dest_y)):
print ( "Yes" )
else :
print ( "No" )
|
Java
class GFG{
static boolean isReachable( long x1, long y1,
long x2, long y2)
{
while (x2 > x1 && y2 > y1)
{
if (x2 > y2)
x2 %= y2;
else
y2 %= x2;
}
if (x2 == x1)
return (y2 - y1) >= 0 &&
(y2 - y1) % x1 == 0 ;
else if (y2 == y1)
return (x2 - x1) >= 0 &&
(x2 - x1) % y1 == 0 ;
else
return false ;
}
public static void main(String[] args)
{
long source_x = 2 , source_y = 10 ;
long dest_x = 26 , dest_y = 12 ;
if (isReachable(source_x, source_y,
dest_x, dest_y))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
C#
using System;
class GFG{
static bool isReachable( long x1, long y1,
long x2, long y2)
{
while (x2 > x1 &&
y2 > y1)
{
if (x2 > y2)
x2 %= y2;
else
y2 %= x2;
}
if (x2 == x1)
return (y2 - y1) >= 0 &&
(y2 - y1) % x1 == 0;
else if (y2 == y1)
return (x2 - x1) >= 0 &&
(x2 - x1) % y1 == 0;
else
return false ;
}
public static void Main(String[] args)
{
long source_x = 2, source_y = 10;
long dest_x = 26, dest_y = 12;
if (isReachable(source_x, source_y,
dest_x, dest_y))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isReachable(x1, y1, x2, y2)
{
while (x2 > x1 && y2 > y1)
{
if (x2 > y2)
x2 %= y2;
else
y2 %= x2;
}
if (x2 == x1)
return (y2 - y1) >= 0 &&
(y2 - y1) % x1 == 0;
else if (y2 == y1)
return (x2 - x1) >= 0 &&
(x2 - x1) % y1 == 0;
else
return false ;
}
let source_x = 2, source_y = 10;
let dest_x = 26, dest_y = 12;
if (isReachable(source_x, source_y,
dest_x, dest_y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
07 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...