Kangaroo challenge

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 x_1 and moves at a rate of v_1 meters per jump. The second kangaroo starts at location x_2 and moves at a rate of v_2 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 x_1, v_1, x_2, and v_2.

Constraints
0 \le x_1 \textless x_2 \le 10000
1 \le v_1 \le 10000
1 \le v_2 \le 10000

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

Sample Output 0

Explanation 0
The two kangaroos jump through the following sequence of locations:
0 \rightarrow 3 \rightarrow 6 \rightarrow 9 \rightarrow  12
4 \rightarrow  6 \rightarrow 8 \rightarrow 10 \rightarrow 12
Thus, the kangaroos meet after 4 jumps and we print YES.

Sample Input 1

Sample Output 1

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., x_2 \textgreater x_1). Because the second kangaroo moves at a faster rate (meaning v_2 \textgreater v_1) 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 i amount of time, the following must be true: x_1 + i \times v_1 == x_2 + i \times v_2. In other words, x_2 - x_1 == (v_1 - v_2)\times i. Thus, if (x_2 - x_1) % (v_1 - v_2) == 0 we return YES otherwise we return NO.

The full code is listed below.

Full code

 

lucas