Files
HackerRank-Solutions/General Programming/Basic Programming/Kangaroo/Solution.java
T

34 lines
1.3 KiB
Java
Raw Normal View History

2017-03-10 01:10:37 -06:00
// Author: Rodney Shaghoulian
2018-07-27 16:45:56 -07:00
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag
2017-03-10 01:10:37 -06:00
import java.util.Scanner;
2017-04-05 18:42:35 -05:00
// Case 1: v1 <= v2
// In this case, kangaroo 1 can never catch up to kangaroo 2
2017-03-18 22:17:22 -05:00
2017-04-05 18:42:35 -05:00
// Case2 : v1 > v2
// x2 - x1 represents the distance between the 2 kangaroos
// v2 - v1 represents the difference in velocities between the 2 kangaroos. It also represents how much closer the kangaroos get in each step (until they either meet or 1 kangaroo jumps over the other)
// If the distance between the kangaroos (x2 - x1) is divisible by how much closer they get in each step (v2 - v1), then they will land at the same location at the same time. Otherwise, 1 kangaroo will jump over the other and they will never be at the same location at the same time.
2017-03-18 22:17:22 -05:00
2017-03-10 01:10:37 -06:00
public class Solution {
2017-09-11 09:32:17 -07:00
public static void main(String[] args) {
2017-04-05 18:42:35 -05:00
/* Save input */
2017-03-10 01:10:37 -06:00
Scanner scan = new Scanner(System.in);
int x1 = scan.nextInt();
int v1 = scan.nextInt();
int x2 = scan.nextInt();
int v2 = scan.nextInt();
scan.close();
/* See if kangaroos meet each other */
if (v1 <= v2) {
System.out.println("NO");
} else {
boolean kangaroosMeet = (x2 - x1) % (v2 - v1) == 0;
System.out.println(kangaroosMeet ? "YES" : "NO");
}
}
}