Problem : Operations
Problem Link : Operation | CodeChef
Difficulty : Easy
Prerequisites : Basic Maths, constructive algorithm
Problem :
Minimum number of operation to make n equal to x.
There are two operations given below.
1.) subtract positive integer from n.
2.) Multiple n with positive integer.
Explanation :
1.) If n is equal to x answer will be 0.
2.) If n is greater than x than we can simply subract (n-x) from n to make it x. Then answer will be 1 operation.
3.) If x is divisible by n than we can simple multiply n by y such that n*y=x. Then answer will be 1 operation.
4.) If neither of the points are satisfy we can make n equal to 1 by subracting (n-1) from n and that will take 1 operation and than multiply 1 with x to make it x so answer will be 2 in this case.
5.) so answer can be 0,1,2 only.
Solution :
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,x;
cin>>n>>x;
if(n==x)
{
cout<<"0\n";
}
else if(n>x)
{
cout<<"1\n";
}
else if(x%n==0)
{
cout<<"1\n";
}
else
{
cout<<"2\n";
}
}
}