Problem
There is a circular track of length consisting of checkpoints and bidirectional roads such that each road has a length of unit.
Chef is currently at checkpoint and wants to reach checkpoint . Find the minimum length of the road he needs to travel.
Input Format
- First line will contain , the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, three integers and - the initial checkpoint, the final checkpoint, and the total number of checkpoints respectively.
Output Format
For each test case, output the minimum length Chef needs to travel.
Constraints
Sample 1:
4 1 3 100 1 98 100 40 30 50 2 1 2
2 3 10 1
Explanation:
Test Case : Chef can go from to as: and then . Thus, total length travelled is units.
Test Case : Chef can go from to as: . Thus, minimum distance travelled is units.
Test Case : Chef can go from to as: . Thus, minimum distance travelled is units.
Test Case : Chef can go from to as: . Thus, minimum distance travelled is unit.
Program :
import java.util.*;
import java.lang.Math.*;
class Codechef
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
long t,a,b,M,i,M1,M2,M3;
t=in.nextLong();
for(i=1;i<=t;i++)
{
a=in.nextLong();
b=in.nextLong();
M=in.nextLong();
if(a!=b&&a>=1&&b<=M&&M>=2)
{
M1=Math.abs(b-a);
M2=Math.abs((M-b)+a);
M3=Math.abs(M-a+b);
if(M1<=M2)
System.out.println(Math.min(M1,M3));
else
System.out.println(M2);
}
}
in.close();
}
}
0 Comments