K-th element of two Arrays

medium

You are given two integer arrays a and b, each sorted in non-decreasing order, and an integer k.

Imagine merging a and b into one combined sorted array. Return the element that would sit at the k-th position (1-indexed) of that merged array.

The merged array is a thought experiment — the best solutions never actually build it. k is guaranteed to be valid: 1 <= k <= a.size() + b.size().

Hints

The combined sorted array is imaginary — you never have to build it. You are being asked for exactly one number: the k-th smallest value across both arrays.
Suppose you take cutA elements from the front of a. Exactly k - cutA must then come from the front of b. The whole problem is now a single unknown integer.
Binary search cutA over the smaller array. A split is correct precisely when the last element taken from each array is <= the first element left behind in the other — and the answer is the larger of the two last-taken elements.

Common doubts

1-indexed: k = 1 asks for the smallest element overall, and k = n + m asks for the largest. The reference solutions return merged[k - 1] in the brute force for exactly this reason.
Yes. That is why the border test uses <= rather than < — with strict inequality, a value present in both arrays can make every split look invalid and derail the binary search.
Then the correct cut sits at 0 or at the full length of an array. The -inf / +inf sentinels make those cuts test cleanly with no special-case branches.
Correctness holds either way, but searching the smaller array bounds the work at O(log (min(n, m))) and keeps cutB = k - cutA in range with the simple bounds max(0, k - m) and min(k, n).

Interview follow-ups

It is this problem in disguise: the median is the k-th element with k = (n + m + 1) / 2 (and for even totals, also k + 1, averaged). The same partition binary search solves it in O(log (min(n, m))).
The two-array partition trick does not extend cleanly. Use a min-heap of the M array fronts and pop k times — O(k log M) — or binary search on the value range, counting elements <= mid across all arrays with M binary searches per step.
Random access dies, so the partition search is out — but the two-pointer merge walk only ever looks at the current front of each stream, making it the right tool: O(k) time, O(1) memory, purely sequential reads.

Fun facts

  • Set k to the middle of the combined length and this problem becomes Median of Two Sorted Arrays — one of the most feared interview classics falls out of this one as a special case.
  • The lazy merge walk in the better solution is the heart of external sorting and of how search engines merge ranked result lists — k-way merging with early stopping shows up everywhere two ordered sources meet.

Asked at

AmazonMicrosoftGoogleFlipkartOracle
Frequently Sometimes Occasionally
Example 1
Input: a[] = [2, 3, 6, 7, 9], b[] = [1, 4, 8, 10], k = 5
Output: 6
The combined sorted array is [1, 2, 3, 4, 6, 7, 8, 9, 10]. Its 5th element is 6.
Example 2
Input: a[] = [1, 4, 8, 10, 12], b[] = [5, 7, 11, 15, 17], k = 6
Output: 10
The combined sorted array is [1, 4, 5, 7, 8, 10, 11, 12, 15, 17]. Its 6th element is 10.
Constraints

- 1 <= a.size(), b.size() <= 10^6 - 0 <= a[i], b[i] <= 10^8 - 1 <= k <= a.size() + b.size() - a and b are each sorted in non-decreasing order

Solve this problem →