• + 80 comments

    I think the formula is messing people up on this. I have found that using words, rather than variable names, to be helpful when writing out the problem.

    So we have two kangaroos starting at different locations, and jumping forward at different distances. If we want to know where any kangaroo is at any given time, there is an intuitive equation for that:

    Kangaroo Position = (Number of Jumps * Distance per Jump) + Starting Position.
    

    We could also write this as:

    K = yv + x
    

    such that K = Kangaroo Position, y = Number of Jumps, v = Distance Per Jump, and x = Starting Position.

    That almost looks like an equation your teacher went over in algebra that one time you were dozing off: y = mx + b. I know we are talking about kangaroos here, but in the background we are really just checking to see when two lines intersect.

    If we have two kangaroos and we want to know when (or if) they will intersect, given their Starting Position and Distance per Jump, the only thing left to solve for is Number of Jumps.

    The kangaroos crossing paths essentially means that Kangaroo Position is equal for both kangaroos. Remember the equation up above K = yv + x? Now that we have two kangaroos, we need to have 2 different equations, and need to determine which value of y (Number of Jumps) can be plugged in to make them equal. So now we have something like this:

    (y * v1) + x1 = (y * v2) + x2
    

    We need to get y on one side of the equation, so we will begin reducing it down.

    (x1 - x2) = (y * v2) - (y * v1)
    
    (x1 - x2) = y(v2 - v1)
    
    (x1 - x2) / (v2 - v1) = y
    

    Luckily, the problem statement gives us the Starting Position and Distance per Jump (x1, x2, v1, and v2) for each kangaroo. When we plug in these numbers it will tell us how many jumps it would take for the kangaroos to end up in the same spot. But not so fast! We can do a a little work up front to check if the kangaroo that is starting in front is moving faster than the kangaroo in the rear i.e we need to see if Distance Per Jump for the kangaroo in front is larger than the one in the rear. If so, then the one in the back will never catch up. Before we even attempt find an intersection we need to ensure that v2 < v1 is true. If this evalutes to false then we are done and the lines will not intersect at any point in the future. If the kangaroos started going the other direction then that would be a different story.

    Anyway, so we plugged the numbers in and we are ready to see how many jumps it will take. At this point there are two scenarios that will occur:

    1. you got a whole number greater than zero
    2. you got a fractional number greater than zero

    In scenario one, this means that after y jumps, the kangaroos will be in the same spot.

    In scenario two, the kangaroos will intersect, but they will be in the air. Kinda cool, but not what we wanted.

    Now we get to the part that seems to mess with peoples heads: the dreaded % operator. Keep reading, and you will see the solution to this problem.

    SPOILER ALERT

    The code below is how we validate that the point of intersection is a whole number.

    (x1 - x2) % (v2 - v1) == 0
    

    The % operator returns the remainder of dividing two numbers. Lets look at an example:

    x1 = 0
    x2 = 4
    v1 = 3
    v2 = 2
    
    (0 - 4) / (2 - 3) => (-4 / -1) => 4 = y
    

    There is no remainder here. So (-4 % -1) == 0 and the kangaroos will intersect after 4 jumps.

    So to put it all together, we need to check the Distance per Jump of the kangaroo in front is less than the one in the rear, and that the equation above gives us a positive integer.

    String response = "NO";
    boolean canCatchUp = (v2 < v1);
    if(canCatchUp) {
        boolean willIntersectOnLand = (x1 - x2) % (v2 - v1) == 0;
        if(willIntersectOnLand) {
            response = "YES";
        }
    }
    
    System.out.println(response);
    
    • + 1 comment

      This is a great writeup, thank you!

      • + 3 comments

        def kangaroo(x1, v1, x2, v2): if x2>x1 and v2>v1: return 'NO' elif (v2-v1) != 0 and (x2-x1)%(v1-v2)==0: return 'YES' else: return 'NO'

        • + 1 comment

          My code was giving false result just in one case, then I saw your code and understood I was missing v1!=v2 condition, thanks

          • + 0 comments

            Welcome

        • + 1 comment

          there should be v2>=v1 instead of v2>v1 because if speed is equal and positions are diffrent then there is no meetup.

          • + 0 comments

            especially for reminder of equal velocity would never meet given different starting positions, thanks.

        • + 0 comments

          even more simple:

          def kangaroo(x1, v1, x2, v2):
                 return 'YES' if v1 > v2 and (x2 - x1)%(v1 - v2) == 0  else 'NO'
          
    • + 0 comments

      Really great explanantion! Thanks!

    • + 1 comment

      That's a very clear & helpful explanation. Thank you.

      • + 1 comment

        Great explanation. Thank you

    • + 0 comments

      clever

    • + 0 comments

      very good explanation.Thank you :)

    • + 1 comment

      this has got no where near enough love.

      where can I get better at recognising these sorts of patterns? or is it just a matter of knowing that when something intersects you have to use y = mx + b ?

      eitherway, nice one! (I just cheated:)

              if(x1 < x2 && v1 < v2) return "NO";
              
              while(x1 <= x2){
                  x1 += v1;
                  x2 += v2;
                  if(x1 == x2)return "YES";
              }
              return "NO";
      
      • + 1 comment

        Intersection is not always in the form of y = mx + b, this is a linear equation involving one variable (the x, or in our case the number of jumps). We could also have a linear equation with 2 variables, we could also have a mixture of linear and non-linear equations (such as an exponential equation and a linear equation and finding their point of intersection).

        Honestly the only shorcut is improving upon math, otherwise you'll be limited by trying to remember certain solutions to equations and when to apply them, but you'll rarely know when NOT to apply them, or which ones to apply for a problem that's a bit more complex :(

        A bit more explanation for this one, the Kangaroos jump at a linear rate (their veolcity/jump movement never changes, it's always v1 or v2). Therefore we have something along the lines of:

        y = velocity * jumps + startLocation

        We are already given velocity AND start location for each kangaroo. let's abbreviate using the actual equation variables (x1,x2,v1,v2), we get two separate equations for y (the total distance travelled after J jumps)

        • y = v1 * J + x1
        • y = v2 * J + x2

        We want to set these equal to each other, this would give us a distance that they both share when their J variable's are equal to one another:

        v1 * J + x1 = v2 * J + x2

        And using our algebra we get the EQ described above in the other posters solution:

        v1 * J - v2 * J + x1 - x2 = 0 Which can be written as: J(v1-v2) = x2 - x1 or (x2-x1) / (v1-v2) = J

        So given 2 velocity values and 2 starting positions, like 4 = v1 and 2 = v2 and 0 = x1 and 4 = x2, we can evaluate:

        • (4 - 0) / (4-2) = J
        • 4 / 2 = J
        • J = 2 (so at 2 jumps we should intersect)

        Showing the values below of the jumps this is right (the far left value is the starting, arrows point to the next jump location):

        • 0 -> 4 -> 8
        • 4 -> 6 -> 8

        And then of course, as the above states, we need to make sure the jump total is a whole number, otherwise we're intersecting in the air. Which is fine, but this problem doesn't really allow that. After all, given any 2 kangaroos if the "BACK" kangaroo is moving FASTER than the one in front of it, they will eventually collide. It's like 2 cars and the one in back is moving faster, if it doesn't slow down it will hit the one in front of it!

        • + 0 comments

          Wow Great

    • + 0 comments

      Fabulous write-up. Thanks!

    • + 0 comments

      wooohooo needed this thank ya!

    • + 0 comments

      This is a brilliant write up, thanks for this.

    • + 0 comments

      Thank you!

    • + 0 comments

      Ruby Implementation:

      def kangaroo(x1, v1, x2, v2)
        return 'NO' unless v2 < v1
        (x1 - x2) % (v2 - v1) == 0 ? 'YES' : 'NO'
      end
      
    • + 0 comments

      brilliant equation. can you explaind deep about it

    • + 1 comment

      You're the real MVP! Here is a Javascript Solution

      After reading your thorough explanation I reached the following solution:

      function kangaroo(x1, v1, x2, v2) {
          return (!(x2 > x1 && v2 > v1) && !(x2 < x1 && v2 < v1) && ((x1 - x2) % (v2 - v1) === 0)) ? 'YES' : 'NO';
      }
      

      I first check if x2 and v2 aren't both greater than x1 and v1 and vice versa. Finally, I check (x1 - x2) % (v2 - v1) === 0). Thanks!

      • + 2 comments

        Brilliant! Can you please explain how the bitwise operators are working for you here?

        • + 1 comment

          Oops! It was a mistake, I don't think it even makes sense to use bitwise operators in these kind of conditions as I'm not even converting anything... Passed all tests though, I'll investigate why. Thanks for pointing it out!

          • + 2 comments

            Thank you. I have always been challeneged by bitwise operations so knowing why that passed will help me overcome the challeges. I will look at it to

            • + 0 comments

              I found this stack overflow question that addresses our problem: https://stackoverflow.com/questions/14871052/why-use-logical-operators-when-bitwise-operators-do-the-same

              Please take a look at all the answers!

            • + 0 comments

              After further testing....

              Imagine you have: x1 = 0; x2 = 3; v1 = 4; v2 = 2;

              console.log(!(x2 > x1 & v2 > v1)); // This returns true

              console.log(x2 > x1 & v2 > v1); // This returns 0

              So after reading through the link I posted in my previous answer, I found out that bitwise operators convert booleans to their respective 32-bit pattern. When something is true it returns 0 else it returns 1.

              That's why in my example, !(0) = true... Still have to find out why this happens though.

    • + 0 comments

      My first comment here on this site, much appreciated for a such a clean write up!

    • + 0 comments

      Thank you!

    • + 0 comments

      I'm in love with such a thoroughly explanation! Thanks.

    • + 0 comments

      Your code passes all the test cases, however the only thing that I don't get is the following:

      If we switch the kangaroos values in an example that outputs YES, they should still intersesct at the same point before switching the values.

      Ex.: // Original: 0 3 4 2 => YES // Switched: 4 2 0 3 = > NO // The first kangaroo is still able to catch the second one and intersect.

      Switching the values of variables shouldn't output a different result.

      Ex.: x = 3; y = 2; // x + y = 5 y = 2; x = 3; // x + y = still 5

    • + 0 comments

      woooh

    • + 0 comments

      Great explaination! Was very helpful, thank you!

    • + 0 comments

      brilliant Logic

    • + 0 comments

      Awesome bro

    • + 0 comments

      Amazingly Created Champ! Thank you! Now understood it well!

    • + 0 comments

      Very clear explanation!

    • + 0 comments

      Awesome explanation! thanks!!

    • + 0 comments

      Nice explanation. Thank you.

    • + 0 comments

      Great explanation!

    • + 0 comments

      Great one!

    • + 0 comments

      Simple and clear explanation.

    • + 0 comments

      Beautifully explained dude.

    • + 0 comments

      Really greate thanks! Well explained :)

    • + 0 comments

      Great explanation dude.

    • + 0 comments

      Thanks for this explaination.

    • + 0 comments

      Great Explanation Thanks.

    • + 0 comments

      Thanks for the explaination

    • + 0 comments
      a, b, c, d = map(int, input().split())
      #print(a, b, c, d)
      
      for i in range(10000):
          a += b
          c += d
          if a == c:
              print('YES')
              break
      else:
          print('NO')
          
      
    • + 0 comments

      Great explanation.Thank you!

    • + 0 comments

      You must a phenomenal teacher in person! Thank you for breaking it down.

    • + 0 comments

      very clear explanation thanks!

    • + 0 comments

      Thank you very much

    • + 0 comments

      Thank you for should a clear explanation.

    • + 1 comment

      I am new to this way of coding and think.. excellent write up there.. I was caught off guard when you said , I know we are talking about kangaroos here, but in the background we are really just checking to see when two lines intersect. Then the nice theory follows up..

      • + 0 comments

        Thanks

    • + 0 comments

      Thanks.. This is great explanation !!

    • + 0 comments

      Amazing Explanation!!

    • + 0 comments

      Nycc

    • + 1 comment

      Thanks for this help

      • + 0 comments

        Welcome

    • + 1 comment

      superb explanation, thank you!

      • + 0 comments

        Welcome.

    • + 1 comment

      Nice explanation! Thanks!!

      • + 0 comments

        Welcome.

    • + 1 comment

      Great writeup. Hats off to you.

      • + 0 comments

        Thanks

    • + 1 comment

      great work...

      • + 0 comments

        Thnx

    • + 1 comment

      Thank you for the detailed and clear explanation.

      • + 0 comments

        My Pleasure.

    • + 1 comment

      Thank You so much, this is a great explanation

      • + 0 comments

        My Pleasure.

    • + 1 comment

      super!!

      • + 0 comments

        Thanks!

    • + 1 comment

      Wow! Thank's for detailed explanation :)

      • + 0 comments

        Welcome

    • + 0 comments

      I never that it could be this much simple. Using his equation ans in javascript

      return (x1-x2) / (v2-v1)? "YES" : "NO";

    • + 0 comments

      Amazing. Thanks!

    • + 0 comments

      goood explation

    • + 0 comments

      Nice explaination. Thanks!

    • + 0 comments

      Thanks Sir you explained it very well (Y)

    • + 0 comments

      The python interpretation for this would be: Since if it enteres the if statements and is satisfied, then it will return yes, and if it is not satisfied, then either way we return No, which is why we place the return no aligned to the first if statement

      if(v1>v2):
      
          if (x1-x2)%(v2-v1)==0:
      
              return('YES')
      
      return('NO')
      
    • + 0 comments

      Thank u for the explanation

    • + 0 comments

      Very well explained , now I can sleep peacefully.

    • + 0 comments

      Awesome, thank you so much for this.

    • + 0 comments

      def kangaroo(x1, v1, x2, v2): if (x2>x1 and v2>v1): return('NO') elif((v1>v2) and ((x1 - x2) % (v2 - v1) == 0)): return("YES") else: return("NO")

      thank you so much for the explanation

    • + 0 comments

      This explanation is so details. Thank you for writing this

    • + 1 comment
      [deleted]
      • + 0 comments

        can I solve this problem like this?

        function linejump(x1,v1,x2,v2){ let j=4; let distanceX1=(v1*j)+x1; let distanceX2=(v2*j)+x2; if(distanceX1%distanceX2==0){ console.log("YES"); }else{ console.log("NO"); } } linejump(0,3,4,2);

    • + 0 comments

      oh wow i didn't even think that way, this is very clever, thanks

    • + 0 comments

      this is so helpful and great explaination!! thanks.

    • + 0 comments

      Thank you, It was a great explanation.

    • + 0 comments

      Thank you for the explanation!!!!

    • + 0 comments
      string kangaroo(int x1, int v1, int x2, int v2) {
          return vector<string>({"NO", "YES"}).at((v2 < v1) && ((x1 - x2)%(v2 - v1) == 0));
      }
      
    • + 0 comments

      return v1>v2 && (x2-x1)%(v1-v2) == 0? "YES" : "NO";

    • + 0 comments

      Thank you soo soo much for this wonderful explanation means a lot.

    • + 0 comments

      Hi Morgan, thanks for explaining it greatly. Now I can understand the equation and guess I really need learn Mathematics so I can apply it to my logic haha.

      There is one problem though, it returned 'NO' when:

      x1 = 2
      v1 = 1
      x2 = 1
      v2 = 2
      

      I think the problem is when you ensure that v2 < v1 is true. While the above will return false, both will jump at the same place after one jump and it should be 'YES'. So, instead of ensuring v2 < v1, we can perform the equation for both kangaroos with y value absoluted (and rounded, if necessary) and then compare it.

      Also we need to ensure that v2 == v1 so that the value don't turn into infinity.

      So the code will be like tihs, in JavaScript (Spoiler Alert):

      let numberOfJumps = Math.abs((x1 - x2) / (v2 - v1));
      let positionAfterJump = (x, v, y) => {
              return x + (v * y);
      };
              
      if (positionAfterJump(x1, v1, numberOfJumps) === positionAfterJump(x2, v2, numberOfJumps) && !(v2 === v1)) {
      	return 'YES';
      }
          
      return 'NO';
      
    • + 0 comments

      Great great explanation Sir

    • + 0 comments

      amazing explanation

    • + 0 comments

      Great explaination dude! loved it

    • + 0 comments

      this is great explanation now i get it