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.
v1 <= v2. In this case, kangaroo 1 can never catch up to kangaroo 2
Case2 :
x2 - x1 represents the distance between the 2 kangaroos
v2 - v1 represents the difference in velocities between the 2 kangaroos. It also represents how much closer the kangaroos get in each step (until they either meet or 1 kangaroo passes the other)
If the distance between the kangaroos (x2 - x1) is divisible by how much closer they get in each step (v2 - v1), then they will land at the same location at the same time. Otherwise, 1 kangaroo will jump over the other and they will never be at the same location at the same time.
importjava.util.Scanner;publicclassSolution{publicstaticvoidmain(String[]args){/* Read and save input */Scannerscan=newScanner(System.in);intx1=scan.nextInt();intv1=scan.nextInt();intx2=scan.nextInt();intv2=scan.nextInt();scan.close();/* See if kangaroos meet each other */if(v1<=v2){System.out.println("NO");}else{booleankangaroosMeet=(x2-x1)%(v2-v1)==0;System.out.println(kangaroosMeet?"YES":"NO");}}}
Let me know if you have any questions.
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 →
Java solution - passes 100% of test cases
From my HackerRank solutions.
Case 1:
v1 <= v2. In this case, kangaroo 1 can never catch up to kangaroo 2
Case2 :
x2 - x1 represents the distance between the 2 kangaroos
v2 - v1 represents the difference in velocities between the 2 kangaroos. It also represents how much closer the kangaroos get in each step (until they either meet or 1 kangaroo passes the other)
If the distance between the kangaroos (x2 - x1) is divisible by how much closer they get in each step (v2 - v1), then they will land at the same location at the same time. Otherwise, 1 kangaroo will jump over the other and they will never be at the same location at the same time.
Let me know if you have any questions.