# The lost Cow

Question

My Solution:

#include <iostream>

using namespace std;

int main()
{
int x , y;
cin >> x >> y;// Taking input x and y
int arr[3];
int counter = 0; //declaring an integer counter
while(x < y) //if x is less than y x and y go into the while loop
{
counter++; // incrementing counter to calculate the iterations in the while loop
arr[0] = x + 1;
if(arr[0] == y)
{
answer = counter*(abs(arr[1] - arr[0]) + abs(arr[2] - arr[1])) + abs(arr[0] - x); // this is to find the distance travelled
break;
}
arr[1] = x - 2;
if(arr[1] == y)
{
answer = counter*(abs(arr[1] - arr[0]) + abs(arr[2] - arr[1])) + abs(arr[1] - arr[0]);
break;
}
arr[2] = x + 4;
if(arr[2] == y)
{
answer = counter*(abs(arr[1] - arr[0]) + abs(arr[2] - arr[2]));
break;
}
}
}


this does not give any outputâ€¦

I also doubt if I have understood the question correctly.

Could you please comment your code so we know what it actually does?

I edited it with comment
Could please tell me how to approach this question?

isnâ€™t there an editorial?

Maybe theyâ€™re trying to figure it out on their own with just a little nudge in the right direction.

Uhmm can anybody help me?
What I thought:

John starts
1.position x on number line

2.position x-2 on number line

1. position x + 4 on number line

This goes on till x is equal to y

When x is equal to y,
My program then counts the distance covered

What is wrong with my algorithm or in my code/?

When x is exactly equal to y? What if y is say, 3?

\text{I have written most of the code...uhm can you please tell me once why} x = 3 and y = 6 \text{gives use output} 9?

We first go to 4, 1, then 7.
3 -> 4 = 1 unit
4 -> 1 = 3 units
1 -> 6 = 5 units
Total is 9 units of distance

Could you pls elaborate it?

My C++ Code:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream fin("lostcow.in");
ofstream fout("lostcow.out");
int x , y;
fin >> x >> y;
int distance = 0;
int counter = 0;
while(x != y)
{
counter++;
if(counter > 1)
{
x++;
}
if(x + 1 == y)
{
distance++;
fout << distance;
break;
}
distance++;
if(x - 2 == y)
{
distance += 2;
fout << distance;
break;
}
distance += 2;
if(x + 4 == y)
{
distance += 4;
fout << distance;
break;
}
distance += 4;
}
}


Iâ€™m not sure how I would elaborate furtherâ€¦

I mean what we do after we get 4, 1 and 7

What do you mean? We simply output the total distance travelled before Bessie is found.

I am confused In this part

How did you get this bold thing below?

3 -> 4
4 -> 1
1 -> 6

I mean, first we go from x to x+1, then from x to x-2, so 3 - 2 = 1
Then we go from x to x + 4, so 3 + 4 = 7
However, 6 is in that range, so we stop at 6.

1 Like

oh ok thanks! I misunderstood the question

I used the method you toldâ€¦this is the code I wrote but it is not giving output:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream fin("lostcow.in");
ofstream fout("lostcow.out");
int x , y;
fin >> x >> y;
int distance = 0;
do
{
distance++;
if(x + 1 == y)
{
fout << distance;
break;
}
distance += 2;
if(x - 2 == y)
{
fout << distance;
break;
}
distance += 4;
if(x + 4 == y)
{
fout << distance;
break;
}
x = x + 4;
}while(x != y);

return 0;
}