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.
Number Line Jumps
Number Line Jumps
Sort by
recency
|
3910 Discussions
|
Please Login in order to post a comment
Here is my c++ solution, you can have the explanation here : https://youtu.be/bRVVCCmXN0Y
//Java Code if(v2>=v1) { return "NO"; } else { if((x2-x1)%(v1-v2)==0) { return "YES"; } else { return "NO"; } }
char* kangaroo(int x1, int v1, int x2, int v2) { while (1) { // Conditions where the kangaroos cannot meet if ((x1 > x2 && v1 > v2) || (x1 < x2 && v1 < v2) || (x1 != x2 && v1 == v2)) { return "NO"; }
}
Just solve it as a simple equation: x1 + (i * v1) = x2 + (i * v2) where i is the number of jumps. This becomes: i * ( v1 - v2) = x2 - x1. It will be 'YES' if i is a positive integer.