Skip to content

Instantly share code, notes, and snippets.

@ooade
Last active August 27, 2016 11:24
Show Gist options
  • Select an option

  • Save ooade/7cc1e2c1b7c9373d5c7f3db517bb4cdb to your computer and use it in GitHub Desktop.

Select an option

Save ooade/7cc1e2c1b7c9373d5c7f3db517bb4cdb to your computer and use it in GitHub Desktop.
Hackerrank Implementation Challenge (Kangaroo)
function main() {
var x1_temp = readLine().split(' ');
var x1 = parseInt(x1_temp[0]);
var v1 = parseInt(x1_temp[1]);
var x2 = parseInt(x1_temp[2]);
var v2 = parseInt(x1_temp[3]);
// return NO, if the first or second kangaroo is far ahead of each other.
if ((x2 > x1 && v2 > v1) || (x1 > x2 && v1 > v2)) {
console.log("NO");
return;
}
function kangaMove(k1, k2, n = 0) {
if (n === 10000) {
return "NO";
}
else if (k1 === k2) {
return "YES";
}
n1 = k1 + v1; // Kangaroo1 next move;
n2 = k2 + v2; // Kangaroo2 next move;
return kangaMove(n1, n2, n + 1);
}
console.log(kangaMove(x1, x2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment