Infinity 2k20

#include
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
ll t;
cin>>t;
while(t–)
{
ll x,y;
cin>>x>>y;
if(x==y)
cout<<0<<endl;
else if(x>=0 && y>=0)
{
if((y-x)>0)
{
if((y-x)%2==0)
cout<<2<<endl;
else
cout<<1<<endl;
}
else if((y-x)<0)
{
if(((abs(y-x))%2)!=0)
cout<<2<<endl;
else
cout<<1<<endl;
}
}
// else if((x%2)==0 && (x>0) && ((y%2)!=0 && y<0))
// cout<<2<<endl;
// else if((x%2)!=0 && (x>0) && ((y%2)==0 && y<0))
// cout<<2<<endl;
// else if((x%2)==0 && (x<0) && ((y%2)==0 && y>0))
// cout<<2<<endl;
// else
// cout<<1<<endl;
else if((x<0 && y<0) || (x<0 || y<0))
{
if((x%2)==0 && (x>0) && ((abs(y))%2)!=0 && (y<0))
cout<<2<<endl;
else if((abs(x))%2==0 && (x<0) && ((abs(y))%2)!=0 && (y<0))
cout<<2<<endl;
else if(((abs(x))%2)!=0 && (x<0) && (y%2)==0 && (y>0))
cout<<1<<endl;
else if(((abs(x))%2)!=0 && (x<0) && ((abs(y))%2==0) && (y<0))
cout<<1<<endl;

   else if((abs(x%2))!=0 && (x<0) && (((abs(y))%2)!=0) && (y>0))
        cout<<2<<endl;
   else if((abs(x))%2==0 && (x<0) && (y%2)==0 && (y>0))
        cout<<2<<endl;
        
   else if((x%2)!=0 && (x>0) && ((abs(y))%2==0) && (y<0))
        cout<<2<<endl;
   else
        cout<<1<<endl;
   }
   
}
return 0;

}
This is first one of infinity 2k20 can someone tell where I have missed some cases. and how to improve it?

First off, this code is ugly as hell. Work on your implementation skill.

Now, I’ll give you a few hints which you can utilize to solve this problem:

HINT 1: Is solving for X > Y and X < Y the same?
HINT 2: How does parity of X and Y affect the overall result?
HINT 3: There are 3 possible answers to a query (excluding 0).

If you are able to break it into cases, you’ll get going in no time. Happy solving!

1 Like

Here u missed the case when the answer will be 3.
That is the case when ur Y>X and (Y-X)/2==even then ur answer is 3 else if it is odd answer is 2.
For e.g. let X=0 and Y =40.Here you can’t reach to 40 by adding up ‘a’ because ur ‘a’ must be odd integer.So here what you can do is simply adding up 25 in one step and another 25 in other step. Now ur X will be 50(i.e.0+25+25==50).Now in the third step u can simply subtract X by (b==10). So at the end ur X when subtracted by 10(50-10==40) which is the required Y. Hope it helps you.

1 Like

I will improve it

1 Like

Thanks

1 Like