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 **10 ^{18}**.

**Examples:**

Input:X1 = 2, Y1 = 10, X2 = 26, Y2 = 12Output: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 = 12Output: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++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Check if (x2, y2) can be reached` `// from (x1, y1)` `bool` `isReachable(` `long` `long` `x1, ` `long` `long` `y1,` ` ` `long` `long` `x2, ` `long` `long` `y2)` `{` ` ` `while` `(x2 > x1 && y2 > y1) {` ` ` `// Reduce x2 by y2 until it is` ` ` `// less than or equal to x1` ` ` `if` `(x2 > y2)` ` ` `x2 %= y2;` ` ` `// Reduce y2 by x2 until it is` ` ` `// less than or equal to y1` ` ` `else` ` ` `y2 %= x2;` ` ` `}` ` ` `// If x2 is reduced to x1` ` ` `if` `(x2 == x1)` ` ` `// Check if y2 can be` ` ` `// reduced to y1 or not` ` ` `return` `(y2 - y1) >= 0` ` ` `&& (y2 - y1) % x1 == 0;` ` ` `// If y2 is reduced to y1` ` ` `else` `if` `(y2 == y1)` ` ` `// Check if x2 can be` ` ` `// reduced to x1 or not` ` ` `return` `(x2 - x1) >= 0` ` ` `&& (x2 - x1) % y1 == 0;` ` ` `else` ` ` `return` `0;` `}` `// Driver Code` `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;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Check if (x2, y2) can be reached` `# from (x1, y1)` `def` `isReachable(x1, y1, x2, y2):` ` ` `while` `(x2 > x1 ` `and` `y2 > y1):` ` ` `# Reduce x2 by y2 until it is` ` ` `# less than or equal to x1` ` ` `if` `(x2 > y2):` ` ` `x2 ` `%` `=` `y2` ` ` `# Reduce y2 by x2 until it is` ` ` `# less than or equal to y1` ` ` `else` `:` ` ` `y2 ` `%` `=` `x2` ` ` `# If x2 is reduced to x1` ` ` `if` `(x2 ` `=` `=` `x1):` ` ` `# Check if y2 can be` ` ` `# reduced to y1 or not` ` ` `return` `(y2 ` `-` `y1) >` `=` `0` `and` `(` ` ` `y2 ` `-` `y1) ` `%` `x1 ` `=` `=` `0` ` ` `# If y2 is reduced to y1` ` ` `elif` `(y2 ` `=` `=` `y1):` ` ` `# Check if x2 can be` ` ` `# reduced to x1 or not ` ` ` `return` `(x2 ` `-` `x1) >` `=` `0` `and` `(` ` ` `x2 ` `-` `x1) ` `%` `y1 ` `=` `=` `0` ` ` `else` `:` ` ` `return` `0` `# Driver Code` `source_x ` `=` `2` `source_y ` `=` `10` `dest_x ` `=` `26` `dest_y ` `=` `12` `# Function call` `if` `(isReachable(source_x, source_y,` ` ` `dest_x, dest_y)):` ` ` `print` `(` `"Yes"` `)` `else` `:` ` ` `print` `(` `"No"` `)` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement` `// the above approach` `class` `GFG{` `// Check if (x2, y2) can be reached` `// from (x1, y1)` `static` `boolean` `isReachable(` `long` `x1, ` `long` `y1,` ` ` `long` `x2, ` `long` `y2)` `{` ` ` `while` `(x2 > x1 && y2 > y1) ` ` ` `{` ` ` `// Reduce x2 by y2 until it is` ` ` `// less than or equal to x1` ` ` `if` `(x2 > y2)` ` ` `x2 %= y2;` ` ` `// Reduce y2 by x2 until it is` ` ` `// less than or equal to y1` ` ` `else` ` ` `y2 %= x2;` ` ` `}` ` ` `// If x2 is reduced to x1` ` ` `if` `(x2 == x1)` ` ` `// Check if y2 can be` ` ` `// reduced to y1 or not` ` ` `return` `(y2 - y1) >= ` `0` `&& ` ` ` `(y2 - y1) % x1 == ` `0` `;` ` ` `// If y2 is reduced to y1` ` ` `else` `if` `(y2 == y1)` ` ` `// Check if x2 can be` ` ` `// reduced to x1 or not` ` ` `return` `(x2 - x1) >= ` `0` `&& ` ` ` `(x2 - x1) % y1 == ` `0` `;` ` ` `else` ` ` `return` `false` `;` `}` `// Driver Code` `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"` `);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Check if (x2, y2) can be reached` `// from (x1, y1)` `static` `bool` `isReachable(` `long` `x1, ` `long` `y1,` ` ` `long` `x2, ` `long` `y2)` `{` ` ` `while` `(x2 > x1 && ` ` ` `y2 > y1) ` ` ` `{` ` ` `// Reduce x2 by y2 ` ` ` `// until it is less ` ` ` `// than or equal to x1` ` ` `if` `(x2 > y2)` ` ` `x2 %= y2;` ` ` `// Reduce y2 by x2 ` ` ` `// until it is less ` ` ` `// than or equal to y1` ` ` `else` ` ` `y2 %= x2;` ` ` `}` ` ` `// If x2 is reduced to x1` ` ` `if` `(x2 == x1)` ` ` `// Check if y2 can be` ` ` `// reduced to y1 or not` ` ` `return` `(y2 - y1) >= 0 && ` ` ` `(y2 - y1) % x1 == 0;` ` ` `// If y2 is reduced to y1` ` ` `else` `if` `(y2 == y1)` ` ` `// Check if x2 can be` ` ` `// reduced to x1 or not` ` ` `return` `(x2 - x1) >= 0 && ` ` ` `(x2 - x1) % y1 == 0;` ` ` `else` ` ` `return` `false` `;` `}` `// Driver Code` `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"` `);` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(1) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Queries to check if a path made up of even numbers from source to destination exists in a Matrix
- Minimum edges to reverse to make path from a source to a destination
- Source to destination in 2-D path with fixed sized jumps
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest Path with even number of Edges from Source to Destination
- Minimize steps defined by a string required to reach the destination from a given source
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Number of elements from the array which are reachable after performing given operations on D
- Reaching a point using clockwise or anticlockwise movements
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimize the sum of product of two arrays with permutations allowed
- Job Scheduling with two jobs allowed at a time
- Minimum cost to reach a point N from 0 with two different operations allowed
- Check if array contains contiguous integers with duplicates allowed
- Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Find n-th element in a series with only 2 digits (4 and 7) allowed

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.