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.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x1 = in.nextInt();
int v1 = in.nextInt();
int x2 = in.nextInt();
int v2 = in.nextInt();
Let me explain how I do it. Let y1 be the total amounts of jumps that kangaroo 1 does so we have:
x1 + y1*v1 = S1, the distance that kangoroo 1 will get to after jumping y1 times.
Similarly, x2 + y2*v2 = S2, the distance that kangoroo 2 will get to after jumping y2 times.
Question: 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?
so it means they will meet when S1 = S2 =>
x1 + y1*v1 = x2 + y2*v2, now we assume that both kangaroo did the same jump then met each other at the same location so the equation now is
x1 + y*v1 = x2 + y*v2 <=> y = (x1 - x2)/(v2 - v1)
now can you guess what is the condtion for y is base on the above equation?
that is how I understand it but I may be wrong and sorry for my bad explaination, English is not my first lanquage.
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 →
public static void main(String[] args) { Scanner in = new Scanner(System.in); int x1 = in.nextInt(); int v1 = in.nextInt(); int x2 = in.nextInt(); int v2 = in.nextInt();
Let me explain how I do it. Let y1 be the total amounts of jumps that kangaroo 1 does so we have:
x1 + y1*v1 = S1, the distance that kangoroo 1 will get to after jumping y1 times.
Similarly, x2 + y2*v2 = S2, the distance that kangoroo 2 will get to after jumping y2 times.
Question: 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?
so it means they will meet when S1 = S2 => x1 + y1*v1 = x2 + y2*v2, now we assume that both kangaroo did the same jump then met each other at the same location so the equation now is
x1 + y*v1 = x2 + y*v2 <=> y = (x1 - x2)/(v2 - v1) now can you guess what is the condtion for y is base on the above equation?
that is how I understand it but I may be wrong and sorry for my bad explaination, English is not my first lanquage.