<p>I start with a simple but not quite linear-time algorithm. We choose some value between <code>array1[0]+array2[0]</code> and <code>array1[N-1]+array2[N-1]</code>. Then we determine how many pair sums are greater than this value and how many of them are less. This may be done by iterating the arrays with two pointers: pointer to the first array incremented when sum is too large and pointer to the second array decremented when sum is too small. Repeating this procedure for different values and using binary search (or one-sided binary search) we could find Kth largest sum in O(N log R) time, where N is size of the largest array and R is number of possible values between <code>array1[N-1]+array2[N-1]</code> and <code>array1[0]+array2[0]</code>. This algorithm has linear time complexity only when the array elements are integers bounded by small constant.</p>

<p>Previous algorithm may be improved if we stop binary search as soon as number of pair sums in binary search range decreases from O(N<sup>2</sup>) to O(N). Then we fill auxiliary array with these pair sums (this may be done with slightly modified two-pointers algorithm). And then we use quickselect algorithm to find Kth largest sum in this auxiliary array. All this does not improve worst-case complexity because we still need O(log R) binary search steps. What if we keep the quickselect part of this algorithm but (to get proper value range) we use something better than binary search?</p>

<p>We could estimate value range with the following trick: get every second element from each array and try to find the pair sum with rank <code>k/4</code> for these half-arrays (using the same algorithm recursively). Obviously this should give some approximation for needed value range. And in fact slightly improved variant of this trick gives range containing only O(N) elements. This is proven in following paper: <a href="http://www.cse.yorku.ca/~andy/pubs/X%2BY.pdf">"Selection in X + Y and matrices with sorted rows and columns" by A. Mirzaian and E. Arjomandi</a>. This paper contains detailed explanation of the algorithm, proof, complexity analysis, and pseudo-code for all parts of the algorithm except <a href="http://en.wikipedia.org/wiki/Quickselect">Quickselect</a>. If linear worst-case complexity is required, Quickselect may be augmented with <a href="http://en.wikipedia.org/wiki/Median_of_medians">Median of medians</a> algorithm.</p>

<p>This algorithm has complexity O(N). If one of the arrays is shorter than other array (M &lt; N) we could assume that this shorter array is extended to size N with some very small elements so that all calculations in the algorithm use size of the largest array. We don't actually need to extract pairs with these "added" elements and feed them to quickselect, which makes algorithm a little bit faster but does not improve asymptotic complexity.</p>

<p>If k &lt; N we could ignore all the array elements with index greater than k. In this case complexity is equal to O(k). If N &lt; k &lt; N(N-1) we just have better complexity than requested in OP. If k > N(N-1), we'd better solve the opposite problem: k'th smallest sum.</p>

<p>I uploaded simple C++11 implementation to <a href="http://ideone.com/qe1YHA">ideone</a>. Code is not optimized and not thoroughly tested. I tried to make it as close as possible to pseudo-code in linked paper. This implementation uses <code>std::nth_element</code>, which allows linear complexity only on average (not worst-case).</p>

<hr>

<p>A completely different approach to find K'th sum in linear time is based on priority queue (PQ). One variation is to insert largest pair to PQ, then repeatedly remove top of PQ and instead insert up to two pairs (one with decremented index in one array, other with decremented index in other array). And take some measures to prevent inserting duplicate pairs. Other variation is to insert all possible pairs containing largest element of first array, then repeatedly remove top of PQ and instead insert pair with decremented index in first array and same index in second array. In this case there is no need to bother about duplicates.</p>

<p>OP mentions O(K log K) solution where PQ is implemented as max-heap. But in some cases (when array elements are evenly distributed integers with limited range and linear complexity is needed only on average, not worst-case) we could use O(1) time priority queue, for example, as described in this paper: <a href="http://arxiv.org/pdf/physics/0606226">"A Complexity O(1) Priority Queue for Event Driven Molecular Dynamics Simulations" by Gerald Paul</a>. This allows O(K) expected time complexity.</p>

<p>Advantage of this approach is a possibility to provide first K elements in sorted order. Disadvantages are limited choice of array element type, more complex and slower algorithm, worse asymptotic complexity: O(K) > O(N).</p>
How to find pair with kth largest sum?
<p>Given two sorted arrays of numbers, we want to find the pair  with the kth largest possible sum. (A pair is one element from the first array and one element from the second array). For example, with arrays</p>

<ul>
<li>[2, 3, 5, 8, 13]</li>
<li>[4, 8, 12, 16]</li>
</ul>

<p>The pairs with largest sums are</p>

<ul>
<li>13 + 16 = 29</li>
<li>13 + 12 = 25</li>
<li>8 + 16 = 24</li>
<li>13 + 8 = 21</li>
<li>8 + 12 = 20</li>
</ul>

<p>So the pair with the 4th largest sum is (13, 8). How to find the pair with the kth largest possible sum?</p>

<p>Also, what is the fastest algorithm? The arrays are already sorted and sizes M and N.</p>

<hr>

<p>I am already aware of the <strong>O(Klogk)</strong> solution , using Max-Heap  given <a href="https://stackoverflow.com/questions/5212037/find-the-kth-largest-sum-in-two-arrays">here</a> .</p>

<p>It also is one of the favorite <em>Google</em> interview question , and they demand a <strong>O(k) solution</strong> .</p>

<p>I've also read somewhere that there exists a <strong>O(k)</strong> solution, which i am unable to figure out .</p>

<p>Can someone explain the correct solution with a pseudocode .</p>

<p>P.S.
Please DON'T post <a href="http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1132204952;start=25#47" rel="nofollow noreferrer">this</a> link as answer/comment.It DOESN'T contain the answer.</p>