Hackerrank – Kangaroo

Hackerrank – Problem Statement

A description of the problem can be found on Hackerrank.


There are two checks:

  • I checked if


    is less then


    and immediatelly print "NO". That means, kangaroo 1 would be never able to reach kangaroo 2.

  • I calculated differences between kangaroo positions


    and their jumps


    . If

        \[x_d % v_d == 0\]

    than kangaroo 1 will reach kangaroo 2. Else "NO".

I created solution in:

All solutions are also available on my GitHub.





2 thoughts on “Hackerrank – Kangaroo

  1. Hi, i tried a variation of your code as such:
    function kangaroo(x1, v1, x2, v2) {
    // Complete this function
    var count = 0;
    if(x2>x1 && v2 > v1){
    return “NO”;

    var bedaX = Math.abs(x1-x2);
    var bedaV = Math.abs(v1-v2);
    if(bedaX % bedaV == 0){
    return “YES”;
    return “NO”;
    return “NO”;
    and it works, but may I ask how what do you mean by “if vd” in your description above, and why does the 1st kangaroo catch up with the 2nd one if the modulus between the difference in X and V is 0?

    1. First. I updated the description because I made a mistake in modulo. It was v_d % x_d == 0. Now it is x_d % v_d == 0.
      Second, this modulo equation is saying that I will find if two kangaroos would eventually meet on an arbitrary position x. You could find the same result calculating all next kangaroos positions. But with it you are able to find infinite kangaroo jumps, when they always jump next to each other without meeting on the same position

      For example, imagine that kangaroo1 starts at x1 = 1 with v1 = 4 and kangaroo2 starts at x2 = 4 with v2 = 2. Their x_d = 3 and v_d = 2. We see that v_d % x_d != 0 => 3 % 2 = 1, so they should do not met.
      Let’s see “brute force” solution:
      First jumps x1 = 5, x2 = 6
      Second jumps x1 = 9, x2 = 8
      Kangaroo1 is already before Kangaroo2 (they did not meet) and it is faster than kangaroo2. They will never meet.

      If you change that kangaroo1 starts at x1 = 1 with v1 = 4 and kangaroo2 starts at x2 = 5 with v2 = 2 using same calculations, you will see that it proves v_d % x_d == 0

Leave a Reply

Your email address will not be published. Required fields are marked *