[Solution] Circular Track solution codechef

Circular Track solution codechef – There is a circular track of length MM consisting of MM checkpoints and MM bidirectional roads such that each road has a length of 11 unit.

[Solution] Circular Track solution codechef

Circular Track solution codechef

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

Input Format

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

[Solution] Circular Track solution codechef

  • 1T10001≤T≤1000
  • 2M1092≤M≤109
  • 1A,BM1≤A,B≤M
  • ABA≠B

Sample Input 1 

1 3 100
1 98 100
40 30 50
2 1 2

Sample Output 1 


[Solution] Circular Track solution codechef Explanation

Test Case 11: Chef can go from 11 to 33 as: 121→2 and then 232→3. Thus, total length travelled is 22 units.

Test Case 22: Chef can go from 11 to 9898 as: 9899100198←99←100←1. Thus, minimum distance travelled is 44 units.

Test Case 33: Chef can go from 4040 to 3030 as: 303132394030←31←32←⋯←39←40. Thus, minimum distance travelled is 1010 units.

Test Case 44: Chef can go from 22 to 11 as: 121←2. Thus, minimum distance travelled is 11 unit.

For Solution

Click here

Leave a Comment