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
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
- 1≤T≤10001≤T≤1000
- 2≤M≤1092≤M≤109
- 1≤A,B≤M1≤A,B≤M
- A≠BA≠B
Sample Input 1
4
1 3 100
1 98 100
40 30 50
2 1 2
Sample Output 1
2
3
10
1
[Solution] Circular Track solution codechef Explanation
Test Case 11: Chef can go from 11 to 33 as: 1→21→2 and then 2→32→3. Thus, total length travelled is 22 units.
Test Case 22: Chef can go from 11 to 9898 as: 98←99←100←198←99←100←1. Thus, minimum distance travelled is 44 units.
Test Case 33: Chef can go from 4040 to 3030 as: 30←31←32←⋯←39←4030←31←32←⋯←39←40. Thus, minimum distance travelled is 1010 units.
Test Case 44: Chef can go from 22 to 11 as: 1←21←2. Thus, minimum distance travelled is 11 unit.