Check if possible to move from given coordinate to desired coordinate

Given two coordinates (x, y) and (a, b). Find if it is possible to reach (x, y) from (a, b).

Only possible moves from any coordinate (i, j) are

  • (i-j, j)
  • (i, i-j)
  • (i+j, j)
  • (i, i+j)

Given x, y, a, b can be negative.



Examples:

Input : (x, y) = (1, 1) and  (a, b) = (2, 3).
Output : Yes.
(1, 1) -> (2, 1) -> (2, 3).

Input : (x, y) = (2, 1) and  (a, b) = (2, 3).
Output : Yes.

Input : (x, y) = (35, 15) and  (a, b) = (20, 25).
Output : Yes.
(35, 15) -> (20, 15) -> (5, 15) -> (5, 10) -> (5, 5) ->
(10, 5) -> (15, 5) -> (20, 5) -> (20, 25)

If we take a closer look at the problem, we can notice that the moves are similar steps of Euclidean algorithm for finding GCD. So, it is only possible to reach coordinate (a, b) from (x, y) if GCD of x, y is equal to GCD of a, b. Otherwise, it is not possible.

Let GCD of (x, y) be gcd. From (x, y), we can reach (gcd, gcd) and from this point, we can reach to (a, b) if and only if GCD of ‘a’ and ‘b’ is also gcd.

Below is the implementation of this approach:

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if it is possible to reach
// (a, b) from (x, y).
#include <bits/stdc++.h>
using namespace std;
  
// Returns GCD of i and j
int gcd(int i, int j)
{
    if (i == j)
        return i;
  
    if (i > j)
        return gcd(i - j, j);
    return gcd(i, j - i);
}
  
// Returns true if it is possble to go to (a, b)
// from (x, y)
bool ispossible(int x, int y, int a, int b)
{
    // Find absolute values of all as sign doesn't
    // matter.
    x = abs(x), y = abs(y), a = abs(a), b = abs(b);
  
    // If gcd is equal then it is possible to reach.
    // Else not possible.
    return (gcd(x, y) == gcd(a, b));
}
  
// Driven Program
int main()
{
    // Converting coordinate into positive integer
    int x = 35, y = 15;
    int a = 20, b = 25;
    (ispossible(x, y, a, b)) ? (cout << "Yes") : (cout << "No");
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if it is possible
// to reach (a, b) from (x, y).
class GFG {
  
    // Returns GCD of i and j
    static int gcd(int i, int j)
    {
        if (i == j)
            return i;
  
        if (i > j)
            return gcd(i - j, j);
        return gcd(i, j - i);
    }
  
    // Returns true if it is possble to go to (a, b)
    // from (x, y)
    static boolean ispossible(int x, int y, int a, int b)
    {
  
        // Find absolute values of all as
        // sign doesn't matter.
        x = Math.abs(x);
        y = Math.abs(y);
        a = Math.abs(a);
        b = Math.abs(b);
  
        // If gcd is equal then it is possible to reach.
        // Else not possible.
        return (gcd(x, y) == gcd(a, b));
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Converting coordinate into positive integer
        int x = 35, y = 15;
        int a = 20, b = 25;
        if (ispossible(x, y, a, b))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
// This code is contributed by Anant Agarwal.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check if it is possible to reach
# (a, b) from (x, y).
# Returns GCD of i and j
def gcd(i, j):
    if (i == j):
        return i
   
    if (i > j):
        return gcd(i - j, j)
    return gcd(i, j - i)
   
# Returns true if it is possble to go to (a, b)
# from (x, y)
def ispossible(x, y, a, b):
    # Find absolute values of all as sign doesn't
    # matter.
    x, y, a, b = abs(x), abs(y), abs(a), abs(b)
   
    # If gcd is equal then it is possible to reach.
    # Else not possible.
    return (gcd(x, y) == gcd(a, b))
   
# Driven Program
    # Converting coordinate into positive integer
x, y = 35, 15
a, b = 20, 25
if(ispossible(x, y, a, b)):
    print "Yes"
else:
    print "No"
# Contributed by Afzal Ansari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if it is possible
// to reach (a, b) from (x, y).
using System;
  
class GFG {
  
    // Returns GCD of i and j
    static int gcd(int i, int j)
    {
        if (i == j)
            return i;
  
        if (i > j)
            return gcd(i - j, j);
        return gcd(i, j - i);
    }
  
    // Returns true if it is possble 
    // to go to (a, b) from (x, y)
    static bool ispossible(int x, int y,
                           int a, int b)
    {
  
        // Find absolute values of all as
        // sign doesn't matter.
        x = Math.Abs(x);
        y = Math.Abs(y);
        a = Math.Abs(a);
        b = Math.Abs(b);
  
        // If gcd is equal then it is possible 
        // to reach. Else not possible.
        return (gcd(x, y) == gcd(a, b));
    }
  
    // Driver code
    public static void Main()
    {
  
        // Converting coordinate
        // into positive integer
        int x = 35, y = 15;
        int a = 20, b = 25;
          
        if (ispossible(x, y, a, b))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if it 
// is possible to reach
// (a, b) from (x, y).
  
// Returns GCD of i and j
function gcd($i, $j)
{
    if ($i == $j)
        return $i;
  
    if ($i > $j)
        return gcd($i - $j, $j);
    return gcd($i, $j - $i);
}
  
// Returns true if it is 
// possble to go to (a, b)
// from (x, y)
function ispossible($x, $y, $a, $b)
{
      
    // Find absolute values
    // of all as sign doesn't
    // matter.
    $x = abs($x); 
    $y = abs($y);
    $a = abs($a);
    $b = abs($b);
  
    // If gcd is equal then
    // it is possible to reach.
    // Else not possible.
    return (gcd($x, $y) == gcd($a, $b));
}
  
// Driver Code
{
      
    // Converting coordinate 
    // into positive integer
    $x = 35; $y = 15;
    $a = 20; $b = 25;
    if (ispossible($x, $y, $a, $b)) 
        echo( "Yes");
    else
        echo( "No");
    return 0;
}
  
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

Yes

This article is contributed by Anuj Chauhan(anuj0503). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.