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 is not optimal due to two reasons:
1. you have a minimum of 10,000 loops (which for many problems will take way longer than they need to solve)
2. Suppose it takes 10,001 steps for the kangaroos to be on the same spot. This might not be a mathematically possible case given the constraints, but for a bigger problem size it might.
I think in general it is not good to introduce arbitrary large numbers for looping through, given the possibility of potential weird subtle bugs when scaling the code up. (Although of course this is just a practice problem so doesn't necessarily apply).
Given all that, you can still keep most of your code with the change of the for(10000) into a while (x1 < x2). This suggests that once the left kangaroo passes the right kangaroo, they'll never meet because they have fixed velocities. There might be some other subtle changes you need, but this is a good starting point.
Good luck!
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 is not optimal due to two reasons: 1. you have a minimum of 10,000 loops (which for many problems will take way longer than they need to solve) 2. Suppose it takes 10,001 steps for the kangaroos to be on the same spot. This might not be a mathematically possible case given the constraints, but for a bigger problem size it might. I think in general it is not good to introduce arbitrary large numbers for looping through, given the possibility of potential weird subtle bugs when scaling the code up. (Although of course this is just a practice problem so doesn't necessarily apply).
Given all that, you can still keep most of your code with the change of the for(10000) into a while (x1 < x2). This suggests that once the left kangaroo passes the right kangaroo, they'll never meet because they have fixed velocities. There might be some other subtle changes you need, but this is a good starting point. Good luck!