Kth Element of Two Sorted Arrays
‘ROW1’ : [2, 5, 8, 17] and ‘ROW2’ : [1, 4, 8, 13, 20]
If ‘ROW1’ is [2, 5, 8, 17] and ‘ROW2’ is [1, 4, 8, 13, 20], then Ninja picks the first plates from each rows, plate containing 2 ladoos from ‘ROW1’ and a plate containing 1 ladoo from ‘ROW2’. Then he gives the plate with 1 Ladoo to the first person in line and places the other plate back to its position.
The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case contains three single space-separated integers ‘N’, ‘M’ and ‘K’ where ‘N’ and ‘M’ denote the number of plates containing ladoos in ‘ROW1’ and ‘ROW2’ respectively and ‘K’ denotes the ‘K’th’ person in line waiting to be served. The second line of each test case contains ‘N’ single space-separated integers, denoting the number of ladoos in each plate of the first row i.e. ‘ROW1’. The third line of each test case contains ‘M’ single space-separated integers, denoting the number of ladoos in each plate of the second row i.e. ‘ROW2’.
For each test case, print an integer denoting the number of ladoos the K'th person will get. 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 <= 100 1 <= N, M, K <= 10^5 K <= (N + M) 0 <= ROW1[i], ROW2[i] <= 10^5 where ROW1[i] and ROW2[i] denote the number of Ladoos in i’th plates of ROW1 and ROW2 respectively. Time Limit: 1 second
First of all, let’s think about what we want to find. We are given two sorted arrays i.e. ‘ROW1’ and ‘ROW2’. In these two arrays, we want to find the number of ladoos the ‘K'th’ person will get. More specifically, we want to find the ‘K’th’ smallest element in the combined and sorted array. The simplest way to find the ‘K’th’ smallest element is to first merge the two arrays and then directly retrieve the ‘K’th’ element.
Here is the algorithm :
Ninja And The Strictly Increasing Array
Negative To The End
Sort 0s, 1s, 2s
Fake Coin Problem