Possible Path

Adam is standing at a point  in an infinite 2D grid. He wants to know if he can reach the point  or not. The only operation he can do is to move to point  from some point . It is given that he can move to any point on this 2D grid, i.e., the points having positive or negative (or ) coordinates.

Tell Adam whether he can reach  or not.

Example:

Input:  a = 2, b = 1, x = 2, y = 3
Output: YES

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string solve(long along blong xlong y)
{
    if (__gcd(ab) == __gcd(xy))
        return "YES";
    else
        return "NO";
}

int main()
{

    long a = 2b = 1;
    long x = 2y = 3;

    cout << solve(abxy<< "\n";

    return 0;
}


No comments:

Post a Comment