Circular Track Solution

Problem

There is a circular track of length M consisting of M checkpoints and M bidirectional roads such that each road has a length of 1 unit.

Chef is currently at checkpoint A and wants to reach checkpoint B. Find the minimum length of the road he needs to travel.

Input Format

  • First line will contain T, the number of test cases. Then the test cases follow.
  • Each test case contains a single line of input, three integers A, B, and M - 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

  • 1 \leq T \leq 1000
  • 2 \leq M \leq 10^9
  • 1 \leq A, B \leq M
  • A \neq B

Sample 1:

Input
Output
4
1 3 100
1 98 100
40 30 50
2 1 2
2
3
10
1

Explanation:

Test Case 1: Chef can go from 1 to 3 as: 1 \rightarrow 2 and then 2 \rightarrow 3. Thus, total length travelled is 2 units.

Test Case 2: Chef can go from 1 to 98 as: 98 \leftarrow 99 \leftarrow 100 \leftarrow 1. Thus, minimum distance travelled is 3 units.

Test Case 3: Chef can go from 40 to 30 as: 30 \leftarrow 31 \leftarrow 32 \leftarrow \dots \leftarrow 39 \leftarrow 40. Thus, minimum distance travelled is 10 units.

Test Case 4: Chef can go from 2 to 1 as: 1 \leftarrow 2. Thus, minimum distance travelled is 1 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();

}

}

Post a Comment

0 Comments