Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
There are two checks:
- I checked if
is less then
and immediatelly print
"NO"
. That means, kangaroo 1 would be never able to reach kangaroo 2. - I calculated differences between kangaroo positions
and their jumps
. If
than kangaroo 1 will reach kangaroo 2. Else
"NO"
.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.*; public class Kangaroo { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int x1 = stdin.nextInt(); int v1 = stdin.nextInt(); int x2 = stdin.nextInt(); int v2 = stdin.nextInt(); System.out.println(isSameLocation(x1, v1, x2, v2)); stdin.close(); } private static String isSameLocation(int x1, int v1, int x2, int v2) { if(v1 < v2) return "NO"; if(x1 < x2 && v1 > v2) { int xDiff = Math.abs(x1 - x2); int vDiff = Math.abs(v1 - v2); if(xDiff % vDiff == 0) { return "YES"; } else { return "NO"; } } return "NO"; } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
'use strict'; const processData = input => { const lines = input.split('\n'); const [x1, v1, x2, v2] = lines[0].split(' ').map(i => parseInt(i)); console.log(sameLocation(x1, v1, x2, v2)); }; const sameLocation = (x1, v1, x2, v2) => { if(v1 < v2) return "NO"; if(x1 < x2 && v1 > v2) { const xDiff = Math.abs(x1 - x2); const vDiff = Math.abs(v1 - v2); if(xDiff % vDiff == 0) { return "YES"; } else { return "NO"; } } return "NO"; }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
object Kangaroo extends App { val lines = Source.stdin.getLines().toList val numbers = lines.head.split(" ").map(_.toInt) val kangaroo1 = (numbers(0), numbers(1)) val kangaroo2 = (numbers(2), numbers(3)) println(if(sameLocation(kangaroo1, kangaroo2)) "YES" else "NO") def sameLocation(k1: (Int, Int), k2: (Int, Int)): Boolean = { val x1 = k1._1 val x2 = k2._1 val v1 = k1._2 val v2 = k2._2 if(v1 < v2) return false if(x1 < x2 && v1 > v2) { val xDiff = (x1 - x2).abs val vDiff = (v1 - v2).abs if(xDiff % vDiff == 0) { return true } else { return false } } false } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def same_location(x1, v1, x2, v2) return "NO" if(v1 < v2) if(x1 < x2 && v1 > v2) xDiff = (x1 - x2).abs vDiff = (v1 - v2).abs if(xDiff % vDiff == 0) return "YES" else return "NO" end end "NO" end (x1, v1, x2, v2) = gets.strip.split(" ").map {|i| i.to_i} puts same_location(x1, v1, x2, v2) |
Hi, i tried a variation of your code as such:
//
function kangaroo(x1, v1, x2, v2) {
// Complete this function
var count = 0;
if(x2>x1 && v2 > v1){
return “NO”;
}
if(x1v2){
var bedaX = Math.abs(x1-x2);
var bedaV = Math.abs(v1-v2);
if(bedaX % bedaV == 0){
return “YES”;
}else{
return “NO”;
}
}else{
return “NO”;
}
}
//
and it works, but may I ask how what do you mean by “if vd” in your description above, and why does the 1st kangaroo catch up with the 2nd one if the modulus between the difference in X and V is 0?
First. I updated the description because I made a mistake in modulo. It was
v_d % x_d == 0
. Now it isx_d % v_d == 0
.Second, this modulo equation is saying that I will find if two kangaroos would eventually meet on an arbitrary position
x
. You could find the same result calculating all next kangaroos positions. But with it you are able to find infinite kangaroo jumps, when they always jump next to each other without meeting on the same positionFor example, imagine that kangaroo1 starts at
x1 = 1
withv1 = 4
and kangaroo2 starts atx2 = 4
withv2 = 2
. Theirx_d = 3
andv_d = 2
. We see thatv_d % x_d != 0 => 3 % 2 = 1
, so they should do not met.Let’s see “brute force” solution:
First jumps
x1 = 5
,x2 = 6
Second jumps
x1 = 9
,x2 = 8
Kangaroo1 is already before Kangaroo2 (they did not meet) and it is faster than kangaroo2. They will never meet.
If you change that kangaroo1 starts at
x1 = 1
withv1 = 4
and kangaroo2 starts atx2 = 5
withv2 = 2
using same calculations, you will see that it provesv_d % x_d == 0