Number Line Jumps - HackerRank || HackerRank Number Line Jumps Solution
Problem Name: Number Line Jumps
Judge: HackerRank
Problem link: Number Line Jumps
HackerRank Number Line Jumps Solution in C
#include <stdio.h>
int main()
{
int x1, v1, x2, v2;
scanf("%d %d %d %d", &x1, &v1, &x2, &v2);
if (x2 > x1 && v2 > v1) printf("NO\n");
else if (v1 == v2) printf("NO\n");
else
{
if ((x1 - x2) % (v2 - v1) == 0) printf("YES\n");
else printf("NO\n");
}
return 0;
}
HackerRank Number Line Jumps Solution in C++/Cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x1, v1, x2, v2, i;
cin >> x1 >> v1 >> x2 >> v2;
if (x2 > x1 && v2 > v1) cout << "NO" << endl;
else if (v1 == v2) cout << "NO" << endl;
else
{
if ((x1 - x2) % (v2 - v1) == 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
HackerRank Number Line Jumps Solution in Java
import java.util.Scanner;
public class Kangaroo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x1 = input.nextInt();
int v1 = input.nextInt();
int x2 = input.nextInt();
int v2 = input.nextInt();
if (x2 > x1 && v2 > v1) System.out.println("NO");
else if (v1 == v2) System.out.println("NO");
else {
if ((x1 - x2) % (v2 - v1) == 0) System.out.println("YES");
else System.out.println("NO");
}
}
}
