You are given ‘N’ = 20, and the hidden number is ‘8’. You won't have access to the hidden number and you will have to guess the number ‘8’ using the higherLower function and print it.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and the hidden number.
Output Format:
For each test case, print a single integer representing the hidden number.
Print a separate line for each test case.
1 <= T <= 10
1 <= N <= 10^9
1 <= hidden number <= N
Time Limit: 1 sec
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
2
20 8
5 2
8
2
For the first test case, ‘N’ = 20, and the hidden number is ‘8’. You won't have access to the hidden number and you will have to guess the number ‘8’ and print it.
For the second test case ‘N’ = 5, and the hidden number is ‘8’. You won't have access to the hidden number and you will have to guess the number ‘8’ and print it.
2
10 10
100 1
10
1
Try every number starting with 1
In this approach, we will iterate through every number from 1 until higherLower(n) is equal to 0. Then we return ‘n’ where higherLower(n) is 0.
Algorithm:
O(N), Where N is the integer given
We are iterating from 1 to N which will cost O(N) time. Hence the final time complexity is O(N).
O(1),
We are not using any extra space.