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 think the formula is messing people up on this. I have found that using words, rather than variable names, to be helpful when writing out the problem.
So we have two kangaroos starting at different locations, and jumping forward at different distances. If we want to know where any kangaroo is at any given time, there is an intuitive equation for that:
such that K = Kangaroo Position, y = Number of Jumps, v = Distance Per Jump, and x = Starting Position.
That almost looks like an equation your teacher went over in algebra that one time you were dozing off: y = mx + b. I know we are talking about kangaroos here, but in the background we are really just checking to see when two lines intersect.
If we have two kangaroos and we want to know when (or if) they will intersect, given their Starting Position and Distance per Jump, the only thing left to solve for is Number of Jumps.
The kangaroos crossing paths essentially means that Kangaroo Position is equal for both kangaroos. Remember the equation up above K = yv + x? Now that we have two kangaroos, we need to have 2 different equations, and need to determine which value of y (Number of Jumps) can be plugged in to make them equal. So now we have something like this:
(y*v1)+x1=(y*v2)+x2
We need to get y on one side of the equation, so we will begin reducing it down.
(x1-x2)=(y*v2)-(y*v1)
(x1-x2)=y(v2-v1)
(x1-x2)/(v2-v1)=y
Luckily, the problem statement gives us the Starting Position and Distance per Jump (x1, x2, v1, and v2) for each kangaroo. When we plug in these numbers it will tell us how many jumps it would take for the kangaroos to end up in the same spot. But not so fast! We can do a a little work up front to check if the kangaroo that is starting in front is moving faster than the kangaroo in the rear i.e we need to see if Distance Per Jump for the kangaroo in front is larger than the one in the rear. If so, then the one in the back will never catch up. Before we even attempt find an intersection we need to ensure that v2 < v1 is true. If this evalutes to false then we are done and the lines will not intersect at any point in the future. If the kangaroos started going the other direction then that would be a different story.
Anyway, so we plugged the numbers in and we are ready to see how many jumps it will take. At this point there are two scenarios that will occur:
you got a whole number greater than zero
you got a fractional number greater than zero
In scenario one, this means that after y jumps, the kangaroos will be in the same spot.
In scenario two, the kangaroos will intersect, but they will be in the air. Kinda cool, but not what we wanted.
Now we get to the part that seems to mess with peoples heads: the dreaded % operator. Keep reading, and you will see the solution to this problem.
SPOILER ALERT
The code below is how we validate that the point of intersection is a whole number.
(x1-x2)%(v2-v1)==0
The % operator returns the remainder of dividing two numbers. Lets look at an example:
x1=0x2=4v1=3v2=2(0-4)/(2-3)=>(-4/-1)=>4=y
There is no remainder here. So (-4 % -1) == 0 and the kangaroos will intersect after 4 jumps.
So to put it all together, we need to check the Distance per Jump of the kangaroo in front is less than the one in the rear, and that the equation above gives us a positive integer.
where can I get better at recognising these sorts of patterns? or is it just a matter of knowing that when something intersects you have to use y = mx + b ?
Intersection is not always in the form of y = mx + b, this is a linear equation involving one variable (the x, or in our case the number of jumps). We could also have a linear equation with 2 variables, we could also have a mixture of linear and non-linear equations (such as an exponential equation and a linear equation and finding their point of intersection).
Honestly the only shorcut is improving upon math, otherwise you'll be limited by trying to remember certain solutions to equations and when to apply them, but you'll rarely know when NOT to apply them, or which ones to apply for a problem that's a bit more complex :(
A bit more explanation for this one, the Kangaroos jump at a linear rate (their veolcity/jump movement never changes, it's always v1 or v2). Therefore we have something along the lines of:
y = velocity * jumps + startLocation
We are already given velocity AND start location for each kangaroo. let's abbreviate using the actual equation variables (x1,x2,v1,v2), we get two separate equations for y (the total distance travelled after J jumps)
y = v1 * J + x1
y = v2 * J + x2
We want to set these equal to each other, this would give us a distance that they both share when their J variable's are equal to one another:
v1 * J + x1 = v2 * J + x2
And using our algebra we get the EQ described above in the other posters solution:
v1 * J - v2 * J + x1 - x2 = 0
Which can be written as: J(v1-v2) = x2 - x1 or (x2-x1) / (v1-v2) = J
So given 2 velocity values and 2 starting positions,
like 4 = v1 and 2 = v2 and 0 = x1 and 4 = x2, we can evaluate:
(4 - 0) / (4-2) = J
4 / 2 = J
J = 2 (so at 2 jumps we should intersect)
Showing the values below of the jumps this is right (the far left value is the starting, arrows point to the next jump location):
0 -> 4 -> 8
4 -> 6 -> 8
And then of course, as the above states, we need to make sure the jump total is a whole number, otherwise we're intersecting in the air. Which is fine, but this problem doesn't really allow that. After all, given any 2 kangaroos if the "BACK" kangaroo is moving FASTER than the one in front of it, they will eventually collide. It's like 2 cars and the one in back is moving faster, if it doesn't slow down it will hit the one in front of it!
Oops! It was a mistake, I don't think it even makes sense to use bitwise operators in these kind of conditions as I'm not even converting anything... Passed all tests though, I'll investigate why. Thanks for pointing it out!
So after reading through the link I posted in my previous answer, I found out that bitwise operators convert booleans to their respective 32-bit pattern. When something is true it returns 0 else it returns 1.
That's why in my example, !(0) = true...
Still have to find out why this happens though.
I am new to this way of coding and think.. excellent write up there.. I was caught off guard when you said , I know we are talking about kangaroos here, but in the background we are really just checking to see when two lines intersect. Then the nice theory follows up..
The python interpretation for this would be: Since if it enteres the if statements and is satisfied, then it will return yes, and if it is not satisfied, then either way we return No, which is why we place the return no aligned to the first if statement
if(v1>v2):
if (x1-x2)%(v2-v1)==0:
return('YES')
return('NO')
function linejump(x1,v1,x2,v2){
let j=4;
let distanceX1=(v1*j)+x1;
let distanceX2=(v2*j)+x2;
if(distanceX1%distanceX2==0){
console.log("YES");
}else{
console.log("NO");
}
}
linejump(0,3,4,2);
Hi Morgan, thanks for explaining it greatly. Now I can understand the equation and guess I really need learn Mathematics so I can apply it to my logic haha.
There is one problem though, it returned 'NO' when:
x1=2v1=1x2=1v2=2
I think the problem is when you ensure that v2 < v1 is true. While the above will return false, both will jump at the same place after one jump and it should be 'YES'. So, instead of ensuring v2 < v1, we can perform the equation for both kangaroos with y value absoluted (and rounded, if necessary) and then compare it.
Also we need to ensure that v2 == v1 so that the value don't turn into infinity.
So the code will be like tihs, in JavaScript (Spoiler Alert):
Number Line Jumps
You are viewing a single comment's thread. Return to all comments →
I think the formula is messing people up on this. I have found that using words, rather than variable names, to be helpful when writing out the problem.
So we have two kangaroos starting at different locations, and jumping forward at different distances. If we want to know where any kangaroo is at any given time, there is an intuitive equation for that:
We could also write this as:
such that K = Kangaroo Position, y = Number of Jumps, v = Distance Per Jump, and x = Starting Position.
That almost looks like an equation your teacher went over in algebra that one time you were dozing off:
y = mx + b
. I know we are talking about kangaroos here, but in the background we are really just checking to see when two lines intersect.If we have two kangaroos and we want to know when (or if) they will intersect, given their Starting Position and Distance per Jump, the only thing left to solve for is Number of Jumps.
The kangaroos crossing paths essentially means that Kangaroo Position is equal for both kangaroos. Remember the equation up above
K = yv + x
? Now that we have two kangaroos, we need to have 2 different equations, and need to determine which value of y (Number of Jumps) can be plugged in to make them equal. So now we have something like this:We need to get
y
on one side of the equation, so we will begin reducing it down.Luckily, the problem statement gives us the Starting Position and Distance per Jump (
x1
,x2
,v1
, andv2
) for each kangaroo. When we plug in these numbers it will tell us how many jumps it would take for the kangaroos to end up in the same spot. But not so fast! We can do a a little work up front to check if the kangaroo that is starting in front is moving faster than the kangaroo in the rear i.e we need to see if Distance Per Jump for the kangaroo in front is larger than the one in the rear. If so, then the one in the back will never catch up. Before we even attempt find an intersection we need to ensure thatv2 < v1
istrue
. If this evalutes tofalse
then we are done and the lines will not intersect at any point in the future. If the kangaroos started going the other direction then that would be a different story.Anyway, so we plugged the numbers in and we are ready to see how many jumps it will take. At this point there are two scenarios that will occur:
In scenario one, this means that after
y
jumps, the kangaroos will be in the same spot.In scenario two, the kangaroos will intersect, but they will be in the air. Kinda cool, but not what we wanted.
Now we get to the part that seems to mess with peoples heads: the dreaded
%
operator. Keep reading, and you will see the solution to this problem.SPOILER ALERT
The code below is how we validate that the point of intersection is a whole number.
The
%
operator returns the remainder of dividing two numbers. Lets look at an example:There is no remainder here. So
(-4 % -1) == 0
and the kangaroos will intersect after 4 jumps.So to put it all together, we need to check the Distance per Jump of the kangaroo in front is less than the one in the rear, and that the equation above gives us a positive integer.
This is a great writeup, thank you!
def kangaroo(x1, v1, x2, v2): if x2>x1 and v2>v1: return 'NO' elif (v2-v1) != 0 and (x2-x1)%(v1-v2)==0: return 'YES' else: return 'NO'
My code was giving false result just in one case, then I saw your code and understood I was missing v1!=v2 condition, thanks
Welcome
there should be v2>=v1 instead of v2>v1 because if speed is equal and positions are diffrent then there is no meetup.
especially for reminder of equal velocity would never meet given different starting positions, thanks.
even more simple:
Really great explanantion! Thanks!
That's a very clear & helpful explanation. Thank you.
Great explanation. Thank you
clever
very good explanation.Thank you :)
this has got no where near enough love.
where can I get better at recognising these sorts of patterns? or is it just a matter of knowing that when something intersects you have to use y = mx + b ?
eitherway, nice one! (I just cheated:)
Intersection is not always in the form of y = mx + b, this is a linear equation involving one variable (the x, or in our case the number of jumps). We could also have a linear equation with 2 variables, we could also have a mixture of linear and non-linear equations (such as an exponential equation and a linear equation and finding their point of intersection).
Honestly the only shorcut is improving upon math, otherwise you'll be limited by trying to remember certain solutions to equations and when to apply them, but you'll rarely know when NOT to apply them, or which ones to apply for a problem that's a bit more complex :(
A bit more explanation for this one, the Kangaroos jump at a linear rate (their veolcity/jump movement never changes, it's always v1 or v2). Therefore we have something along the lines of:
y = velocity * jumps + startLocation
We are already given velocity AND start location for each kangaroo. let's abbreviate using the actual equation variables (x1,x2,v1,v2), we get two separate equations for y (the total distance travelled after J jumps)
We want to set these equal to each other, this would give us a distance that they both share when their J variable's are equal to one another:
v1 * J + x1 = v2 * J + x2
And using our algebra we get the EQ described above in the other posters solution:
v1 * J - v2 * J + x1 - x2 = 0 Which can be written as: J(v1-v2) = x2 - x1 or (x2-x1) / (v1-v2) = J
So given 2 velocity values and 2 starting positions, like 4 = v1 and 2 = v2 and 0 = x1 and 4 = x2, we can evaluate:
Showing the values below of the jumps this is right (the far left value is the starting, arrows point to the next jump location):
And then of course, as the above states, we need to make sure the jump total is a whole number, otherwise we're intersecting in the air. Which is fine, but this problem doesn't really allow that. After all, given any 2 kangaroos if the "BACK" kangaroo is moving FASTER than the one in front of it, they will eventually collide. It's like 2 cars and the one in back is moving faster, if it doesn't slow down it will hit the one in front of it!
Wow Great
Fabulous write-up. Thanks!
wooohooo needed this thank ya!
This is a brilliant write up, thanks for this.
Thank you!
Ruby Implementation:
brilliant equation. can you explaind deep about it
You're the real MVP! Here is a Javascript Solution
After reading your thorough explanation I reached the following solution:
I first check if x2 and v2 aren't both greater than x1 and v1 and vice versa. Finally, I check (x1 - x2) % (v2 - v1) === 0). Thanks!
Brilliant! Can you please explain how the bitwise operators are working for you here?
Oops! It was a mistake, I don't think it even makes sense to use bitwise operators in these kind of conditions as I'm not even converting anything... Passed all tests though, I'll investigate why. Thanks for pointing it out!
Thank you. I have always been challeneged by bitwise operations so knowing why that passed will help me overcome the challeges. I will look at it to
I found this stack overflow question that addresses our problem: https://stackoverflow.com/questions/14871052/why-use-logical-operators-when-bitwise-operators-do-the-same
Please take a look at all the answers!
After further testing....
Imagine you have: x1 = 0; x2 = 3; v1 = 4; v2 = 2;
console.log(!(x2 > x1 & v2 > v1)); // This returns true
console.log(x2 > x1 & v2 > v1); // This returns 0
So after reading through the link I posted in my previous answer, I found out that bitwise operators convert booleans to their respective 32-bit pattern. When something is true it returns 0 else it returns 1.
That's why in my example, !(0) = true... Still have to find out why this happens though.
My first comment here on this site, much appreciated for a such a clean write up!
Thank you!
I'm in love with such a thoroughly explanation! Thanks.
Your code passes all the test cases, however the only thing that I don't get is the following:
If we switch the kangaroos values in an example that outputs YES, they should still intersesct at the same point before switching the values.
Ex.: // Original: 0 3 4 2 => YES // Switched: 4 2 0 3 = > NO // The first kangaroo is still able to catch the second one and intersect.
Switching the values of variables shouldn't output a different result.
Ex.: x = 3; y = 2; // x + y = 5 y = 2; x = 3; // x + y = still 5
woooh
Great explaination! Was very helpful, thank you!
brilliant Logic
Awesome bro
Amazingly Created Champ! Thank you! Now understood it well!
Very clear explanation!
Awesome explanation! thanks!!
Nice explanation. Thank you.
Great explanation!
Great one!
Simple and clear explanation.
Beautifully explained dude.
Really greate thanks! Well explained :)
Great explanation dude.
Thanks for this explaination.
Great Explanation Thanks.
Thanks for the explaination
Great explanation.Thank you!
You must a phenomenal teacher in person! Thank you for breaking it down.
very clear explanation thanks!
Thank you very much
Thank you for should a clear explanation.
I am new to this way of coding and think.. excellent write up there.. I was caught off guard when you said , I know we are talking about kangaroos here, but in the background we are really just checking to see when two lines intersect. Then the nice theory follows up..
Thanks
Thanks.. This is great explanation !!
Amazing Explanation!!
Nycc
Thanks for this help
Welcome
superb explanation, thank you!
Welcome.
Nice explanation! Thanks!!
Welcome.
Great writeup. Hats off to you.
Thanks
great work...
Thnx
Thank you for the detailed and clear explanation.
My Pleasure.
Thank You so much, this is a great explanation
My Pleasure.
super!!
Thanks!
Wow! Thank's for detailed explanation :)
Welcome
I never that it could be this much simple. Using his equation ans in javascript
return (x1-x2) / (v2-v1)? "YES" : "NO";
Amazing. Thanks!
goood explation
Nice explaination. Thanks!
Thanks Sir you explained it very well (Y)
The python interpretation for this would be: Since if it enteres the if statements and is satisfied, then it will return yes, and if it is not satisfied, then either way we return No, which is why we place the return no aligned to the first if statement
Thank u for the explanation
Very well explained , now I can sleep peacefully.
Awesome, thank you so much for this.
def kangaroo(x1, v1, x2, v2): if (x2>x1 and v2>v1): return('NO') elif((v1>v2) and ((x1 - x2) % (v2 - v1) == 0)): return("YES") else: return("NO")
thank you so much for the explanation
This explanation is so details. Thank you for writing this
can I solve this problem like this?
function linejump(x1,v1,x2,v2){ let j=4; let distanceX1=(v1*j)+x1; let distanceX2=(v2*j)+x2; if(distanceX1%distanceX2==0){ console.log("YES"); }else{ console.log("NO"); } } linejump(0,3,4,2);
oh wow i didn't even think that way, this is very clever, thanks
this is so helpful and great explaination!! thanks.
Thank you, It was a great explanation.
Thank you for the explanation!!!!
return v1>v2 && (x2-x1)%(v1-v2) == 0? "YES" : "NO";
Thank you soo soo much for this wonderful explanation means a lot.
Hi Morgan, thanks for explaining it greatly. Now I can understand the equation and guess I really need learn Mathematics so I can apply it to my logic haha.
There is one problem though, it returned 'NO' when:
I think the problem is when you ensure that
v2 < v1
istrue
. While the above will return false, both will jump at the same place after one jump and it should be 'YES'. So, instead of ensuringv2 < v1
, we can perform the equation for both kangaroos withy
value absoluted (and rounded, if necessary) and then compare it.Also we need to ensure that
v2 == v1
so that the value don't turn into infinity.So the code will be like tihs, in JavaScript (Spoiler Alert):
Great great explanation Sir
amazing explanation
Great explaination dude! loved it
this is great explanation now i get it