In this Number Line Jumps problem, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump. The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump. You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO.
Problem solution in Python programming.
#!/bin/python3 import sys x1,v1,x2,v2 = input().strip().split(' ') x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)] exist = False while(True): if x1 == x2: exist = True break if (v1 > v2 and x1 > x2) or (v2 > v1 and x2 > x1) or (v2 == v1 and x2 != x1): break x1 += v1 x2 += v2 if exist: print('YES') else: print('NO')
Problem solution in Java Programming.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x1 = in.nextInt(); int v1 = in.nextInt(); int x2 = in.nextInt(); int v2 = in.nextInt(); int vdiff = v1 - v2; if (vdiff <= 0) { System.out.println("NO"); return; } int xdiff = x1 - x2; System.out.println(xdiff % vdiff == 0 ? "YES" : "NO"); } }
Problem solution in C++ programming.
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm> #include <vector> #include <string> #include <queue> #include <stack> #include <set> #include <map> #include <sstream> #include <cmath> #include <ctime> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<string> vs; typedef vector< vector<int> > vvi; #define forn(i, n) for (int i = 0; i < (int)(n); i++) #define forv(i, v) forn(i, v.size()) #define all(v) v.begin(), v.end() #define mp make_pair #define pb push_back int main() { #ifdef NEREVAR_PROJECT freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int x1, v1, x2, v2; cin >> x1 >> v1 >> x2 >> v2; forn(it, 2e8) { if (x1 == x2) { puts("YES"); return 0; } x1 += v1; x2 += v2; } puts("NO"); return 0; }
Problem solution in C programming.
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int x1; int v1; int x2; int v2; int flag = 0; scanf("%d %d %d %d",&x1,&v1,&x2,&v2); int d1 = x1, d2 = x2; if (d1 == d2) { flag = 1; } for (int i = 0; flag == 0 && d1 <= d2; i++) { d1 += v1; d2 += v2; if (d1 == d2) { flag = 1; break; } } if (flag == 1) { printf("YES"); } else { printf("NO"); } return 0; }
Problem solution in JavaScript programming.
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// 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]); if (x1 > x2) { if (v1 >= v2) return console.log('NO'); } if (x2 > x1) { if (v2 >= v1) return console.log('NO'); } var jump = (x2 - x1) / (v2 - v1); console.log(jump % 1 === 0 ? 'YES' : 'NO'); }
Whoever wrote this C++ code should be tried at the Hague. It's a crime against humanity.
I totally agree with you
Python 3:
def kangaroo(x1, v1, x2, v2):
# Write your code here
for i in range(1000000):
x1=x1+v1;
x2=x2+v2;
if(x1==x2):
return "YES"
if i==999999:
return "NO"
PHP codes:
function kangaroo($x1, $v1, $x2, $v2) {
// Write your code here
$output = 'NO';
$vdiff = $v1 – $v2;
if($vdiff <=0){
$output = 'NO';
return $output;
}
$xdiff = $x1 – $x2;
if($xdiff % $vdiff == 0){
$output = 'YES';
}else{
$output = 'NO';
}
return $output;
}
JS (much easier)
function kangaroo(x1, v1, x2, v2) {
let n = 0;
if (v1 < v2) {
console.log('NO'); return 'NO';
} else {
if (n = (x1-x2)%(v2-v1) == 0) {
console.log ('YES');
return 'YES';
} else {console.log('NO'); return 'NO';}
}
}