Practice

MEDIUM

Math

# PROBLEM:

Sachin and Sakshi are Newly Married couple in the town. And since our couple are bit romantic the air in the town is filled up with the love. I hope you can feel it like me. Because they belive that “Love is Life” .

Today our couple is going to buy some Food for the childs living in orfanage. The food items are being sold at price Z rupees per food item. Let’s consider Sachin has A rupees and Sakshi has R rupees with them. Both i.e Sachin and Sakshi will buy as many food items they can from their own money not by sharing each other because both belive in independent idologies. This way each of them will buy an integer non-negative number of food items. But after some time they both came up with good idea and found that the total number of food items they buy can increase (or decrease) if one of them gives several rupees to the other.

(NOTE : The rupee will always strictly be a non-negative integer only).

So Sachin and Sakshi want to exchange with rupees in such a way that they will buy the maximum possible number of food items. so among all possible ways to buy the maximum possible number of food items find such a way that minimizes the number of rupees one can share with other.

Your job is to print the Minimum number of rupees one will share with other to buy the maximum number of food items.

# QUICK EXPLANATION:

Suppose Sachin has 50 rupees and Sakshi has 40 rupees, and the price for one food item is 30 rupees. If they don’t exchange with rupees, they will buy 1+1=2 food items. BUT if Sakshi gives Sachin ten rupee, then Sachin will have 60 rupees, Sakshi will left with 30 rupees, and with this exchange they will buy 2+1=3 food items. (which is more than previous one i.e 2).

# EXPLANATION:

Let’s consider Sachin has A rupees and Sakshi has R rupees with them. Both i.e Sachin and Sakshi will buy as many food items they can from their own money not by sharing each other because both belive in independent idologies. This way each of them will buy an integer non-negative number of food items. But after some time they both came up with good idea and found that the total number of food items they buy can increase (or decrease) if one of them gives several rupees to the other.

Suppose Sachin has 50 rupees and Sakshi has 40 rupees, and the price for one food item is 30 rupees. If they don’t exchange with rupees, they will buy 1+1=2 food items. BUT if Sakshi gives Sachin ten rupee, then Sachin will have 60 rupees, Sakshi will left with 30 rupees, and with this exchange they will buy 2+1=3 food items. (which is more than previous one i.e 2).

(NOTE : The rupee will always strictly be a non-negative integer only).

# SOLUTIONS:

Setter's Solution

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t–)
{
ll a,r,z;
cin>>a>>r>>z;
ll transfer = 0;
if(a%z + r%z >=z)
{
transfer = z - max(a%z , r%z);
}
cout<<transfer<<endl;
}

``````return 0;
``````

}

Tester's Solution

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t–)
{
ll a,r,z;
cin>>a>>r>>z;
ll transfer = 0;
if(a%z + r%z >=z)
{
transfer = z - max(a%z , r%z);
}
cout<<transfer<<endl;
}

``````return 0;
``````

}

Editorialist's Solution

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t–)
{
ll a,r,z;
cin>>a>>r>>z;
ll transfer = 0;
if(a%z + r%z >=z)
{
transfer = z - max(a%z , r%z);
}
cout<<transfer<<endl;
}

``````return 0;
``````

}