
You are given ‘N’ as 7 and ‘K’ as 4. Numbers from 1 to 7 are arranged as [1, 3, 5, 7, 2, 4, 6], and the number at position 4 is 7. So, the answer is 7.
The first line of input contains a single integer ‘T’, representing the number of test cases.
The first line of each test case consists of two space-separated integers, ‘N’ and ‘K’, representing the total number of integers and the position of the integer to find.
For each test case, print the integer present at position ‘K’.
Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^12
1 <= K <= N
Time Limit: 1 sec
In this approach, we will maintain an array to store the numbers. We will insert all the odd numbers from 1 to N in ascending order and then insert all the remaining even numbers in ascending order from 1 to N in the array. To find the element at position K, return the number at position K - 1 in the array (because numbering positions are from 1).
Algorithm:
In this approach, we know the position of the last odd number is at the middle of the array. We will check if K is more than N / 2, then we can find (K - N / 2) even number from 1. Otherwise, if K is less than half of N we can directly find Kth Odd number from 1.
To find any even number at the index position from 1 we can use the following formula:
2*index + 2To find any odd number at the index position from 1 we can use the following formula:
2*index + 1Algorithm: