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.
This problem can be solved purely mathematically, without any iteration.
Assume that there exists some k number of jumps, at which they will meet: x1 + k(v1) = x2 + k(v2)
Solving for k: k = (x2 - x1) / (v1 - v2)
If k would be a positive integer, they will land at the same location in k jumps.
(Note: If k is negative, they would have to hop backwards. If k is not an integer, they will pass eachother while in the air.)
So you just need to evaluate if that function would result in a positive integer. You don't even need to actually determine a value for k. And no iteration is required.
The code for this is left as an exercise for the reader.
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 →
This problem can be solved purely mathematically, without any iteration.
Assume that there exists some k number of jumps, at which they will meet:
x1 + k(v1) = x2 + k(v2)
Solving for k:
k = (x2 - x1) / (v1 - v2)
If k would be a positive integer, they will land at the same location in k jumps.
(Note: If k is negative, they would have to hop backwards. If k is not an integer, they will pass eachother while in the air.)
So you just need to evaluate if that function would result in a positive integer. You don't even need to actually determine a value for k. And no iteration is required.
The code for this is left as an exercise for the reader.