Police and Thief Solution | CodeChef Problem Solution 2022
Chef discovered that his secret recipe has been stolen. He immediately informs the police of the theft.
It is known that the policeman and thief move on the number line. You are given that:
- The initial location of the policeman on the number line is and his speed is units per second.
- The initial location of the thief on the number line is and his speed is unit per second.
Find the minimum time (in seconds) in which the policeman can catch the thief. Note that, the policeman catches the thief as soon as their locations become equal and the thief will try to evade the policeman for as long as possible.
Input Format
- The first line of input will contain an integer — the number of test cases. The description of test cases follows.
- The first and only line of each test case contains two integers and , as described in the problem statement.
Output Format
For each test case, output in a single line the minimum time taken by the policeman to catch the thief.
Constraints
Sample Input 1
3
1 3
2 1
1 1
Sample Output 1
2
1
0
Explanation
Test case : The initial locations of the policeman and thief are and respectively. The minimum time taken by the policeman to catch the thief is seconds, and this happens when both the policeman and the thief move towards the right.
Test case : The initial location of the policeman and thief are and respectively. The minimum time taken by the policeman to catch the thief is second, and this happens when both the policeman and the thief move towards the left.
Test case : The initial locations of the policeman and thief are and respectively. Because the police is already present at the location of thief, the time taken by police to catch the thief is 0 seconds.
No comments:
Post a Comment