#C5145. Find the K-th Smallest Element in Two Sorted Arrays
Find the K-th Smallest Element in Two Sorted Arrays
Find the K-th Smallest Element in Two Sorted Arrays
You are given two sorted arrays arr1 and arr2, and an integer k. Your task is to find the k-th smallest element in the sorted union of these two arrays.
For example, if we merge the arrays and sort them, the element in the k-th position (using 1-based indexing) is our answer. Mathematically, if S is the sorted union and S = {s_1, s_2, \ldots, s_{n+m}}, then you need to find s_k where \(1 \leq k \leq n+m\).
Note: The input arrays are already sorted. Your solution should efficiently merge the arrays virtually until the k-th element is determined.
inputFormat
The first line of the input contains three integers: n
(the number of elements in arr1), m
(the number of elements in arr2), and k
(the position of the element to find, where k is 1-indexed).
The second line contains n
space-separated integers (the elements of arr1).
The third line contains m
space-separated integers (the elements of arr2). If an array is empty, the corresponding line will be empty.
outputFormat
Output a single integer — the k-th smallest element in the sorted union of the two arrays.
## sample5 4 5
1 3 5 7 9
2 4 6 8
5
</p>