Kth element of two sorted lists

Medium

Given two sorted arrays A and B, and another value k, print the kth element of the resultant sorted array.

Example

A: [1, 2, 3, 4, 5, 6]
B: [3, 4, 4, 5, 6, 6]
Result: [1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6]
3rd element in the array is 3.
6th element in the array is 4.

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of independent test cases.

For each test case, the input has four lines:

  • The first line contains two integers ‘n’ and ‘m’ denoting the length of the arrays A and B respectively.
  • The second line contains n space-separated integers denoting the elements of A.
  • The third line contains m space-separated integers denoting the elements of B.
  • The fourth line contains ‘k’ denoting the kth element to print.

Output Format

For each test case, print the kth smallest number in the merged array.

Sample Input

2
4 4
1 2 3 4
2 3 4 5
3
4 5
1 1 2 3
3 3 4 5 6
5

Expected Output

2
3

Constraints

1 <= T <= 100
1 <= n, m <= 10000
1 <= Ai, Bi <= 100000
1 <= k <= n+m

Editorial Link: Editorial