# HackerRank ‘Kangaroo’ Solution

##### Short Problem Definition:

There are two kangaroos on an x-axis 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. Given the starting locations and movement rates for each kangaroo, can you determine if they’ll ever land at the same location at the same time?

Kangaroo

##### Complexity:

time complexity is O(1)

space complexity is O(1)

##### Execution:

There is no need to simulate the movement. We can reason that the two kangaroos either meat at the smallest common multiply or never.

##### Solution:
```use std::io;
fn get_numbers() -> Vec<u32> {
let mut line = String::new();
line.split_whitespace().map(|s| s.parse::<u32>().unwrap()).collect()
}

fn main() {
let numbers = get_numbers();
let x1 = numbers[0];
let v1 = numbers[1];
let x2 = numbers[2];
let v2 = numbers[3];

if x1 == x2 && v1 == v2 {
println!("YES");
}
else if x1 == x2 && v1 > v2 {
println!("NO");
}
else if x1 <= x2 && v1 <= v2 {
println!("NO");
}
else {
if (x2 - x1) % (v1 - v2) == 0  {
println!("YES");
}
else {
println!("NO");
}
}
}
```

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

• Jake Zeledón

Hi. I made it just with one “if” and then i used a “for”. I like your solution
I use Python.
I share my result…

• I thought of simulating the kangaroos first, but decided to go with a O(1) solution. Good to know that it also works! Wanna share your python solution?

• Jake Zeledón

#!/bin/python3

import sys
bl = False
x1,v1,x2,v2 = input().strip().split(‘ ‘)
x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)]
if ((x1>x2 and v1>v2) or (x1<x2 and v1<v2)):
print("NO")
else:
for i in range(9999):
x1 = x1 + v1
x2 = x2 + v2
if x1 == x2 :
bl = True
break

if bl == True:
print("YES")
else:
print("NO")

• Jake Zeledón

#!/bin/python3

import sys
bl = False
x1,v1,x2,v2 = input().strip().split(‘ ‘)
x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)]
if ((x1>x2 and v1>v2) or (x1<x2 and v1<v2)):
print("NO")
else:
for i in range(9999):
x1 = x1 + v1
x2 = x2 + v2
if x1 == x2 :
bl = True
break

if bl == True:
print("YES")
else:
print("NO")

How I didn't see the better way. I made my solution.

• Jake Zeledón

#!/bin/python3

import sys
bl = False
x1,v1,x2,v2 = input().strip().split(‘ ‘)
x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)]
if ((x1>x2 and v1>v2) or (x1<x2 and v1<v2)):
print("NO")
else:
for i in range(9999):
x1 = x1 + v1
x2 = x2 + v2
if x1 == x2 :
bl = True
break

if bl == True:
print("YES")
else:
print("NO")

• Jake Zeledón

This is my solution. It’s works

• I added the html takes to enable syntax highlighting.

• Jake Zeledón

Thanks. 🙂

• Zegar

if x1 == x2 && v1 == v2 {
println!(“YES”);
}
else if x1 == x2 && v1 > v2 {
println!(“NO”);
}

Those checks are not necessary. The task clearly states that x2 has to be higher than x1. The rest is ok 😉

• Harsh Paliwal

#include
#include
#include
#include
#include
#include
#include

int main(){
int x1;
int v1;
int x2;
int v2;
scanf(“%d %d %d %d”,&x1,&v1,&x2,&v2);
if((x2>x1&&v2>v1)||(x1>x2&&v1>v2))
printf(“NO”);
else{
for( ; x1<=10000 && x210000&&x2>10000)
printf(“NO”);}

return 0;
}
Is This Code Ryt Or Not Plzz Tell Me