HackerRank- Kangaroo (easy)

Link to code challenge

Needed assistance on 2nd formula instead of doing an iteration. You first find the difference between the starting points of the two kangaroos. Next, you find the difference between the speed at which each kangaroo travels. This difference is how much the distance between the two kangaroos is decreasing by at each "iteration". Therefore, the difference in speed has to be a factor of the difference in their starting points in order for both kangaroos to eventually be on the same points. If the difference in distance is 7 but at each iteration the distance is reduced by 2, they will never be on the same point (7 / 2 !== 0).

function kangaroo(x1, v1, x2, v2) { let pos1 = x1; let pos2 = x2; if ( (x1 > x2 && v1 > v2) || (x1 < x2 && v1 < v2) ) return("NO"); else if ((x1 - x2) % (v2 - v1) === 0) { return "YES"; } return "NO"; }

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int