CIN001-Editorial

PROBLEM LINK:

Author: Jainam Sogani( jainam_sogani | CodeChef User Profile for Jainam Sogani | CodeChef)
Tester: Jainam Sogani( jainam_sogani | CodeChef User Profile for Jainam Sogani | CodeChef)
Editorialist: Jainam Sogani( jainam_sogani | CodeChef User Profile for Jainam Sogani | CodeChef)

DIFFICULTY:

EASY

PREREQUISITES:

Basic Maths

PROBLEM:

Rachit lives on the coordinate axis OX at x=a. He has his night tuition classes at x=b. On the axis at point x=c, a road light is placed which has it’s radius of coverage as r i.e. from x=c, at a distance less than or equal to r, he will have light on his path, otherwise he has to walk in dark.

Print the distance (the total coordinate points) during which Rachit will walk in dark while going from his home to his tuition. Note that the road light can be located between his home and tuition and even outside it.

Constraints - −10^8≤a,b,c≤10^8 0≤r≤10^8

Input - First line contains the total number of test cases t(1<=t<=1000).

The next line contains four test cases a : the coordinate of his home, b : the coordinate of his tuition classes, c : the coordinate where road light is present and r : it’s coverage radius.

Output - Output an integer - the total coordinate points during which Rachit will walk in the dark during his movement.

QUICK EXPLANATION:

To get the answer, we need to subtract the whole distance and the distance that we will be in the light.

EXPLANATION:

To get the answer, we need to subtract the whole distance and the distance that we will be in the light.
Let the left boundary of the cover be p=c−r, and the right boundary of the cover be q=c+r.
Then the intersection boundaries will be start=max(p,min(a,b)), end=min(q,max(a,b)).
Then the answer is calculated by the formula |b−a|−max(0,ed−st).

SOLUTIONS:

#include
using namespace std;

int main()
{
int t;
cin >> t;

while (t--)
{
	int a, b, c, r;
	cin >> a >> b >> c >> r;

	int p = max(min(a, b), c - r);
	int q = min(max(a, b), c + r);
	cout << max(a, b) - min(a, b) - max(0, q - p) << endl;
}

return 0;

}