There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location and moves at a rate of meters per jump. The second kangaroo starts at location and moves at a rate of meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they’ll ever land at the same location at the same time?

**Input Format**

A single line of four space-separated integers denoting the respective values of , , , and .

**Constraints**

**Output Format**

Print `YES`

if they can land on the same location at the same time; otherwise, print `NO`

.

**Note:** The two kangaroos must land at the same location after making the same number of jumps.

**Sample Input 0**

1 |
0 3 4 2 |

**Sample Output 0**

1 |
YES |

**Explanation 0**

The two kangaroos jump through the following sequence of locations:

Thus, the kangaroos meet after jumps and we print `YES`

.

**Sample Input 1**

1 |
0 2 5 3 |

**Sample Output 1**

1 |
NO |

**Explanation 1**

The second kangaroo has a starting location that is ahead (further to the right) of the first kangaroo’s starting location (i.e., ). Because the second kangaroo moves at a faster rate (meaning ) and is already ahead of the first kangaroo, the first kangaroo will never be able to catch up. Thus, we print `NO`

.

**Solution**

In order for the two kangaroos to land at the same location after amount of time, the following must be true: . In other words, . Thus, if we return `YES`

otherwise we return `NO`

.

The full code is listed below.

**Full code**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class Kangaroo { // condition to meet: x1 + i*v1 == x2 + i*v2 ==> x2 - x1 == (v1 - v2)*i ==> return YES if (x2 -x1) % (v1 - v2) == 0 public static void solve(int x1, int v1, int x2, int v2) { if (v2 >= v1) { System.out.println("NO"); } else { if ((x2 - x1) % (v1 - v2) == 0) { System.out.println("YES"); } else { System.out.println("NO"); } } } public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream(System.getProperty("user.home") + "/" + "in.txt")); Scanner in = new Scanner(System.in); int x1 = in.nextInt(); int v1 = in.nextInt(); int x2 = in.nextInt(); int v2 = in.nextInt(); solve(x1, v1, x2, v2); } } |