Number Line Jumps with JavaScript

Tomlai
2 min readApr 9, 2021

--

Photo by bantersnaps on Unsplash

Today i got some fun algorithm problem from HackerRank to look at. It is call Number Line Jumps. You are choreographing a circus show with various animals. For one act, you are given 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.

You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.

Example

x1 = 2
v1 = 1
x2 = 1
v2 = 2

After one jump, they are both at x=3 , (x1 + v1 =2+1, x2+v2 = 1+2), so the answer is YES.

Thanks to Jake Lira to share his easier understand solution for example.

we set those position x1 and x2 as kangaroo 1 (kan1)and kangaroo2(kan2). second step we make a for loop for set their position and adding up its speed per jump. Therefore, in the for lop as long as the position keep looping we can check those Kan1 and Kan2 position plus their speed of jump.
In the end we can set the if statement to check if Kan1 and Kan2 are equal or not to return the answer as “Yes” or “No”.

--

--