One day Ninja Misha and Ninja Andrew (students of the Ultimate Ninja Ankush) were playing a very simple game. First, each player chooses an integer in the range from 1 to ‘N’. Let's assume that Ninja Misha chose the number ‘numMisha’, and Ninja Andrew chose the number ‘numAndrew’.
Then, by using a random generator they choose a random integer ‘C’ in the range between 1 and ‘N’ (any integer from 1 to ‘N’ is chosen with the same probability), after which the winner is the player whose number was closer to ‘C’. The boys agreed that if ‘numMisha’ and ‘numAndrew’ are located on the same distance from ‘C’, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Ninja Misha, and the number ‘N’. You need to determine which value Ninja Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer ‘numAndrew’ (1 ≤ ‘numAndrew’ ≤ n), that the probability that
| ‘C’ - ‘numAndrew’| < | ‘C’ - ‘numMisha’ | is maximal, where ‘C’ is the equi-probably chosen integer from 1 to ‘N’ (inclusive).
For example:
Given ‘N’ = 4, ‘numMisha’ = 2.
The options available to us are 1, 3, and 4. In this case, choosing 3 would be most optimal because the probability that Andrew wins is 2 / 4, whereas if he chooses 1 or 4 it would be 1 / 4.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines contain 2 space-separated integers ‘N’, the range given to us, and ‘numMisha’ the number Ninja Misha chose.
Output Format :
For each test case, You are supposed to return the number Andrew should choose such that he has a higher probability of winning.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10 ^ 6
1 <= ‘numMisha’ <= ‘N’
Time Limit: 1sec.
2
4 2
3 1
3
2
In the first test case, The options available to us are 1, 3, and 4. In this case, choosing 3 would be most optimal because the probability that Andrew wins is 2 / 4, whereas if he chooses 1 or 4 it would be 1 / 4.
In the second test case, The options available to us are 2 and 3. In this case, choosing 2 would be the most optimal because this is given a probability of 2 / 3 that Andrew might win, whereas choosing 3 given a probability of 1 / 3.
2
5 4
2 1
3
2
Can we find a point such that we are close to most of the numbers?
The idea is to observe that if we choose the median of the first ‘N’ numbers, then we will be close to at least ‘N / 2’ numbers, to that extent, if Misha chose number less than the median than choosing number just next to it would be most optimal since it will give us all the extra numbers between her number and median. Similarly, if she chose a number greater than the median, then choosing a number just less than her would be most optimal.
The steps are as follows:
O(1).
Since we are finding the median, which is a constant time work, the overall time complexity will be O(1).
O(1).
Since we are not using any extra space, the overall space complexity is O(1).