Created
December 9, 2020 16:24
-
-
Save amuradyan/473797dda1338b08e7d8d05bee0ee43c to your computer and use it in GitHub Desktop.
Hackerrank `Number Line Jumps` problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io._ | |
| import java.math._ | |
| import java.security._ | |
| import java.text._ | |
| import java.util._ | |
| import java.util.concurrent._ | |
| import java.util.function._ | |
| import java.util.regex._ | |
| import java.util.stream._ | |
| object Solution { | |
| // Complete the kangaroo function below. | |
| def kangaroo(x1: Int, v1: Int, x2: Int, v2: Int): String = { | |
| if (v1 == v2){ | |
| if (x1 == x2) "YES" else "NO" | |
| } else { | |
| val dv = v1 - v2 | |
| val dx = x2 - x1 | |
| if (dx / dv >= 0 && dx % dv == 0) "YES" else "NO" | |
| } | |
| } | |
| def main(args: Array[String]): Unit = { | |
| val stdin = scala.io.StdIn | |
| val x1V1X2V2 = stdin.readLine.split(" ") | |
| val x1 = x1V1X2V2(0).trim.toInt | |
| val v1 = x1V1X2V2(1).trim.toInt | |
| val x2 = x1V1X2V2(2).trim.toInt | |
| val v2 = x1V1X2V2(3).trim.toInt | |
| val result = kangaroo(x1, v1, x2, v2) | |
| println(result) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment