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.
Number Line Jumps
Number Line Jumps
Sort by
recency
|
3930 Discussions
|
Please Login in order to post a comment
This is my solution in perl
Here is my c++ solution, you can have the explanation here : https://youtu.be/bRVVCCmXN0Y
I hope this helps someone who was confused as to why we needed to calculate the mod.
First of all, as @Mily94 pointed out this problem's solution could be solved using this simply equation:
Simplifying the equation gives us this: j = (x2 - x1) / (v1 - v2) and in order to figure out if 'j' is a whole number (i.e. if there exists a whole number of jumps that would allow our kangaroos to meet), we have to find out if the remainder of this division is 0, which requires the use of the modulo operator. I hope this makes sense.
PHP
def kangaroo(x1, v1, x2, v2): for i in range (0,10001,1): if v1>v2 and (x2-x1)%(v1-v2) == 0 : return "YES" else: return "NO"