# Is there any mistake in that Cpp-code, which can lead unfavorable output?

Is there any mistake in that Cpp-code, which can lead unfavorable output ? Actually while submitting its solution…I got 6 wrong test cases, but I am not able to find any error!

I’m providing question(commented) for reference,

```/* You are choreographing a circus show with various animals. For one act, 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.
*/

#include <iostream>
int main()
{
int x1, v1, x2, v2;

std::cout << "ENTER FIRST STARTING POS THEN ITS SPEED:"<< std::endl;
std::cin >> x1 >> v1;

std::cout << "ENTER SECOND STARTING POS THEN ITS SPEED:"<< std::endl;
std::cin >> x2 >> v2;

int max, min;
(v1 > v2) ? max = x1, min = x2 : max = x2, min = x1;

while ((x1 != x2) && (min > max))
{

if (max == x1)
{
max = max + v1;
min = min + v2;
}
else
{
max = max + v2;
min = min + v1;
}

x1 = x1 + v1;
x2 = x2 + v2;
}

if (max == min)
{
std::cout << "YES";
}

else
std::cout << "NO";

return (0);
}
```

Initial remark: You want to check if there exists an integer n with

`x1 + n * v1 = x2 + n * v2`

IOW

`x1 - x2 = n(v2 - v1)`

Now if `v1 != v2` and `(x1 - x2) / (v2 - v1)` is an integer (or `(x1 - x2) % (v2 - v1) == 0`), the answer is YES. If `v1 == v2` and `x1 == x2`, the answer is also YES In all other cases, the answer is NO.

Trying to calculate this using a loop isn’t exactly an optimal approach.

Now to your code. And I must admit I didn’t spot the mistake yet, but I have a few comments on it.

You terminate your loop as soon as either x1 == x2 or min <= max. We’ll have to think about the meaning of the variables here.

x1 is the position of kangaroo1

x2 is the position of kangaroo2

Obviously, if both are equal, you have a “hit” (answer is YES).

min is the position of the slower kangaroo

max is the position of the faster kangaroo

As a hint, it would be a good idea to choose names that reflect this meaning. min and max aren’t that well chosen. The double bookkeeping doesn’t make it easier to understand what’s going on (you keep the same position in two variables), and the comparison to check which velocity is which doesn’t add to clarity.

Let me rewrite your algorithm a little

```/* You are choreographing a circus show with various animals. For one act, 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.
*/

#include <iostream>
int main()
{
int pos_fast, v_fast, pos_slow, v_slow;
int x1, v1, x2, v2;

std::cout << "ENTER FIRST STARTING POS THEN ITS SPEED:"<< std::endl;
std::cin >> x1 >> v1;

std::cout << "ENTER SECOND STARTING POS THEN ITS SPEED:"<< std::endl;
std::cin >> x2 >> v2;

// find out who's fast and who's slow
if (v1 > v2) {
pos_fast = x1;
v_fast = v1;
pos_slow = x2;
v_slow = v2;
} else {
pos_fast = x2;
v_fast = v2;
pos_slow = x1;
v_slow = v1;
}

// now if v1 == v2, we could end up in an endless loop
// So we won't let them run if they are equally fast
if (v_fast != v_slow) {
// let them jump until the faster kangaroo isn't behind the slower one any more
while (pos_fast < pos_slow) {
pos_fast = pos_fast + v_fast;
pos_slow = pos_slow + v_slow;
}
}

// and if both kangaroos now have a headache, the answer is YES ;-)
if (pos_fast == pos_slow) {
std::cout << "YES";
} else {
std::cout << "NO";
}

return (0);
}
```

Maybe, if you share the test cases where things go wrong, it’ll help me to spot the mistake in your code.