HackerRank: Kangaroo
By: Richard Russell
Tags: Algorithms, Coding, Programming

HackerRank: Kangaroo

Problem

You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity)

The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump

and moves at a rate of meters per jump The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump

You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES . Otherwise, return NO .

For example, kangaroo 1 starts at x1 = 2 with a jump distance of v1 = 1 and kangaroo 2 start at x2= 1 with a jump distance of v2 = 2 . After one jump, they are both at x = 3 , as x1 + v1 = 2+1 = 3, x2 + v2 = 1+2 = 3 , so our answer is YES

Function Description

Complete the function kangaroo in the editor below. It should return YES if they reach the same position at the same time, or NO if they don’t.

The kangaroos have the following parameter(s):

x1, v1 : Integers, starting position and jump distance for kangaroo 1

: Integers, starting position and jump distance for kangaroo 1 x2, v2 : Integers, starting position and jump distance for kangaroo 2

Input Format

A single line of four space-separated integers denoting the respective values of x1, v1, x2, v2 .

Constraints

0 < x1 < x2 < 10000

1 < v1 < 10000

1 < v2 < 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.

Answer and Thoughts

My first approach towards this problem is to eliminate the obvious, which is that if the position of either kangaroo is ahead, and its relative speed is also faster than that of the opposite kangaroo, then it is fairly obvious that the kangaroos will not meet. Doing so, I typed the following code:

def kangaroo(x1, v1, x2, v2):

if x1 > x2 and v1 > v2:

return "NO"

if x1 < x2 and v1 < v2:

return "NO"

else:

return "YES"

What’s interesting from that line of code is that it passes the first two test cases, which would appear while running code. However, it failed 8 test cases when trying to submit. In the process, I thought more about the process of how the kangaroos could meet; whether there was any mathematical equation that could define the process of how they would meet.

Without any idea in mind, I decided to lurk in the Discussions section of the problem, in which I found my answer. If we were to mathematically equate the meeting of the kangaroos, it would more or less look like the following:

x1 + n*v1 = x2 + n*v2

Where n is defined at the number of steps both kangaroos have jumped.

To demonstrate, let us look at an example where the sample input is (0, 3, 4, 2). Plugging the numbers into the equation above would give us the following:

0 + n*3 = 4 + n*2

The equation above would give us an answer that n = 4 , meaning that it would take four jumps in order for the kangaroos to meet. The important aspect to highlight here is that the number of jumps is a whole number, meaning that there isn’t any remainder of any kind.

Using that logic, we can change the equation to find the value of n as follows;

x1 + n*v1 = x2 + n*v2

n*v1 — n*v2 = x2 — x1

n(v1-v2) = x2-x1

n = (x2-x1)/(v1-v2)

Since n needs to be a whole number without any remainder, we can write a code that could find if the modulus of n is equal to zero. So, if (x2-x1)%(v1-v2) == 0 , then we can safely print out YES .

Of course, from that solution alone we can find another problem; if the value of both speeds is the same, then wouldn’t it return a runtime error (dividing by zero is the cardinal sin)? To combat this, I created another if statement that checks to see whether the value of both speeds is the same or different. Doing so lead me to this code:

if v1 == v2:

return "NO"

Before I continue, I have a funny story. As I was finishing up my code, there was one test case that still failed (Test Case #10, a hidden test case). I was already on the verge of losing my sanity, so I unlocked the hidden case to investigate what the problem was. The input was (43, 2, 70, 2). Immediately, I knew that this test case tested the possibility that both speeds are equal. Nevertheless, I was sure that I’ve already written the appropriate conditional statement to combat this. After seeing that my code didn’t give out any output, I rescanned my code, only to see that I had written “NO” as “N”. From this careless mistake, I could have saved 5 hackos just by skimming and debugging over the code again. A lesson in patience.

Another funny-yet-careless mistake I made was forgetting to put an else statement after writing all the proper conditional statements. This led to over half test cases failing. Obviously, another careless mistake that is bound to happen when the saw isn’t sharpened.

In the end, my code is as follows:

Which is admittedly really messy. The conditional statements aren’t branched that well. I suppose that this is time to really get the rusty skills back in shape.

Key Takeaways