“I am not in danger, Skyler. I am the danger. A guy opens his door and gets shot, and you think that of me? No! I am the one who knocks!”
Skyler fears Walter and ponders escaping to Colorado. Walter wants to clean his lab as soon as possible and then go back home to his wife.
In order clean his lab, he has to achieve cleaning level of lab as Y. The current cleaning level of the lab is X.
He must choose one positive odd integer a and one positive even integer b. Note that, he cannot change a or b once he starts cleaning.
He can perform any one of the following operations for one round of cleaning:
Replace X with X+a.
Replace X with X−b.
Find minimum number of rounds (possibly zero) to make lab clean.
Input:
First line will contain T, number of test cases. T testcases follow :
Each test case contains two space separated integers X,Y.
Output:
For each test case, output an integer denoting minimum number of rounds to clean the lab.
Sample Input:
3
0 5
4 -5
0 10000001
Sample Output:
1
2
1
EXPLANATION:
For the first testcase, you can convert X to Y by choosing a=5 and b=2.
It will cost minimum of 1 cleaning round. You can select any other combination of a,b satisfying above condition but will take minimum of 1 cleaning round in any case.
For the second testcase, you can convert X to Y by choosing a=1 and b=10. In first round they will replace X to X+a and then in second round replace to X−b. You can perform only one operation in one round.
Solution:
- Java
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
int x,y,i,t;
Scanner in=new Scanner(System.in);
t=in.nextInt();
for(i=1;i<=t;i++)
{
x=in.nextInt();
y=in.nextInt();
if(x<y)
{
if((y-x)%2!=0)
System.out.println("1");
else if((y-x)%4==0)
System.out.println("3");
else
System.out.println("2");
}
else if(x>y)
{
if((x-y)%2==1)
System.out.println("2");
else
System.out.println("1");
}
else
System.out.println("0");
}
// your code goes here
}
}
|
- C++
#include <iostream>
using namespace std;
int main() {
// your code goes here
long T,X,Y;
cin>>T;
for(long i=0; i<T; i++)
{
cin>>X>>Y;
if(X==Y)
cout<<0<<endl;
else if(X<Y)
{
if((Y-X)%2==1)
cout<<1<<endl;
else if((Y-X)%4!=0)
cout<<2<<endl;
else cout<<3<<endl;
}
else if(X>Y)
{
if((X-Y)%2==1)
cout<<2<<endl;
else cout<<1<<endl;
}
}
return 0;
}
|
- Python 3.6
T=int(input())
for _ in range(T):
X,Y=map(int,input().split())
diff=abs(X-Y)
if(X>Y):
if (diff%2==0):
ans=1
else:
ans=2
elif (X<Y):
if (diff%2!=0):
ans=1
elif (diff%4==0):
ans=3
else:
ans=2
else:
ans=0
print(ans) |
Note:
This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.
Disclaimer:-
The above hole problem statement is given by Codechef, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.
Helps a lot...
ReplyDeleteThank you....