Problem 708
Kth Element of Two Sorted Arrays
Medium · 20 pts · Array, Binary Search, Divide and Conquer
Problem
You are given two arrays sorted in non-decreasing order and an integer K. Find the K-th smallest element (counting from 1, with duplicates counted separately) of the combined array, without actually merging them. Binary search on how many elements to take from the first array: if you take i from the first and K - i from the second, the split is correct when the last taken element of each array is not larger than the first untaken element of the other.
Input
The first line contains N, M and K. The second line contains the first array. The third line contains the second array.
Output
Print the K-th smallest element of the combined arrays.
Constraints
1 <= N, M <= 100000; 1 <= K <= N + M; -10^9 <= values <= 10^9
Example input
5 4 5 2 3 6 7 9 1 4 8 10
Example output
6
Explanation
Combining 2 3 6 7 9 and 1 4 8 10 gives 1 2 3 4 6 7 8 9 10. With K = 5 the fifth smallest value is 6.