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