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.
def kangaroo(x1, v1, x2, v2):
if (v2 >= v1 or (x2-x1)%(v1-v2) != 0):
return 'NO'
return 'YES'
We first check if v2 (second kangaroo's jump) is bigger or equal to v1 (first kangaroo's jump) because there's a constraint that x2 (starting position of the second kangaroo) has to be bigger than x1 (starting position of the first kangaroo). It means that for first kangaroo to reach the second one it has to have bigger jump than the second kangaroo. If the second kangaroo has bigger or equal jump, the first will never catch it, therefore the statement is satisfied and we return a 'NO'.
If the first condition is not satisfied, we check if the combination of starting positions and steps makes it impossible for the first kangaroo to reach the second. If it indeed is impossible (if its modulus doesn't equal zero) we again return a 'NO'.
If none of these happen, we return a 'YES'.
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 →
We first check if v2 (second kangaroo's jump) is bigger or equal to v1 (first kangaroo's jump) because there's a constraint that x2 (starting position of the second kangaroo) has to be bigger than x1 (starting position of the first kangaroo). It means that for first kangaroo to reach the second one it has to have bigger jump than the second kangaroo. If the second kangaroo has bigger or equal jump, the first will never catch it, therefore the statement is satisfied and we return a 'NO'.
If the first condition is not satisfied, we check if the combination of starting positions and steps makes it impossible for the first kangaroo to reach the second. If it indeed is impossible (if its modulus doesn't equal zero) we again return a 'NO'.
If none of these happen, we return a 'YES'.