

‘ARR1’ = {1, 4, 6, 7}
‘ARR2’ = {2, 3, 5, 6}
‘K’ = 6
So the sorted array after merging ‘ARR1’ and ‘ARR2’ will be:
{1, 2, 3, 4, 5, 6, 6, 7}
So the 6th element (1 based indexing) is in the new sorted merged array/list is 6.
Try to do this problem without using any extra space.
The first line of input contains a single integer T’, representing the number of test cases.
Then the ‘T’ test cases follow.
The first line of each test case contains three single space-separated numbers, ‘M’, ‘N’ and ‘K’, denoting the size of ‘ARR1’, the size of ‘ARR2’, and the position of the element in the new sorted array/list.
The second line contains ‘M’ space-separated distinct integers denoting the ‘ARR1’ elements.
The third line contains ‘N’ space-separated distinct integers denoting the ‘ARR2’ elements.
For each test case print a single integer representing the ‘K’th element in the new sorted array.
The output of every test case will be printed in a separate line.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= T <=100
1 <= M,N <= 10^4
1 <= K <= M+N
0 <= ARR1[i]’ , ARR2[i] <= 10^5
Where 'ARR1[i]' and 'ARR2[i]' denote the i'th element of 'ARR1' and 'ARR2' respectively.
Time limit: 1 sec
The idea is very simple. We will make a new array/list say ‘ARR3’ by appending ‘ARR1’ at end of ‘ARR2’ or by appending ‘ARR2’ at the end of ‘ARR1’. We will sort ‘ARR3’ and can easily return the ‘K’th element.
Complete Algorithm:
In the earlier approach, we used an extra array/list ‘ARR3’ to store the elements of both arrays/lists, but we did not make any use of the fact that both arrays are sorted. This time we will traverse both the arrays/lists simultaneously and alternatively. We will traverse the arrays/lists in such a way that it can give us the final sorted array/list.
The idea is to use the divide and conquer technique and decrease the time complexity. So we will recursively call the given function and each time we will reject one half from any one of two arrays according to the circumstances.
Complete Algorithm:
Base Case :
Recursive calls: