0

Kangaroo Jump Logic

There are two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump. The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time? if x1=0, v1= 5, x2=4, v2=4

1st Jan 2018, 6:02 PM
karthick Ramanathan
karthick Ramanathan - avatar
2 Answers
+ 1
public String kangaroo(int x1, int v1, int x2, int v2) { // Complete this function String ans = ""; if(v1 > v2 && ((x2-x1)%(v1-v2) == 0)) { ans ="YES";} else { ans ="NO";} return ans; }
2nd Jan 2018, 2:53 AM
karthick Ramanathan
karthick Ramanathan - avatar
+ 17
i am giving a general solution ::: x1+(v1*t)=x2+(v2*t) //bcz distance in +ve x-dir & time taken will be same for both on putting values we get t=4 sec , ie they will meet first @ t=4sec at dist. 20 m from the origin
1st Jan 2018, 6:19 PM
Gaurav Agrawal
Gaurav Agrawal - avatar