We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I don't know if you still need the answer, but I can help you.
The positions and velocities are just a representation of straight lines, with positions beyng the y axis intercept (distance) and the velocities remain the rate of change of position through time (x axis).
So in the if statement you check for (1) that the lines actually meet, (2) that, being discrete series of dots and not continuous, they meet in the same spot, (3) add the case in which they have same speed but not same starting position (as without this you'll fail test 10).
1) you want that the "order" of positions (x1 > x2 or viceversa) to be different from the one of the velocities ( v1 < v2), or they'll never meet, so you can implement a "sign" function like -------> math.copysign(1,x1-x2) != math.copysign(1, v1-v2) (or == if you test for a 'NO!')
2)you know that the 2 kangaroos start with a relative distance abs(x1-x2) (like this we cover both cases, where x1 < x2 or viceversa) and at every step (being that v1 != v2, check at (3)) the slower of the two fills up a little piece of the initial gap, which can be represented by abs(v1-v2) (again to cover both cases).
This means that they will either cross their lines of movement or they'll meet in a particular spot. How to find out? If the gap filled up everytime by the slower kangaroo (the one with smaller v) is a divider of the initial gap, then they will meet at one spot, otherwise they won't.
abs(x1-x2)%abs(v1-v2) == 0 ---> 'YES!'
3) you have to add that clause to the statement, otherwise the case where they have same speed but also an initial gap will return a wrong answer (giving 'YES!' when they actually won't meet because the gap never gets filled)
This approach saves up a lot of time and I arrived to that through long thinking (very long)... and right after that I saw you can just equate the formulas for the two lines and find if n is an integer ------> x1 + (n * v1) = x2 + (n * v2)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Number Line Jumps
You are viewing a single comment's thread. Return to all comments →
I don't know if you still need the answer, but I can help you. The positions and velocities are just a representation of straight lines, with positions beyng the y axis intercept (distance) and the velocities remain the rate of change of position through time (x axis).
So in the if statement you check for (1) that the lines actually meet, (2) that, being discrete series of dots and not continuous, they meet in the same spot, (3) add the case in which they have same speed but not same starting position (as without this you'll fail test 10).
1) you want that the "order" of positions (x1 > x2 or viceversa) to be different from the one of the velocities ( v1 < v2), or they'll never meet, so you can implement a "sign" function like -------> math.copysign(1,x1-x2) != math.copysign(1, v1-v2) (or == if you test for a 'NO!')
2)you know that the 2 kangaroos start with a relative distance abs(x1-x2) (like this we cover both cases, where x1 < x2 or viceversa) and at every step (being that v1 != v2, check at (3)) the slower of the two fills up a little piece of the initial gap, which can be represented by abs(v1-v2) (again to cover both cases). This means that they will either cross their lines of movement or they'll meet in a particular spot. How to find out? If the gap filled up everytime by the slower kangaroo (the one with smaller v) is a divider of the initial gap, then they will meet at one spot, otherwise they won't. abs(x1-x2)%abs(v1-v2) == 0 ---> 'YES!'
3) you have to add that clause to the statement, otherwise the case where they have same speed but also an initial gap will return a wrong answer (giving 'YES!' when they actually won't meet because the gap never gets filled)
This approach saves up a lot of time and I arrived to that through long thinking (very long)... and right after that I saw you can just equate the formulas for the two lines and find if n is an integer ------> x1 + (n * v1) = x2 + (n * v2)