Two independent sensors each emit a sorted (non-decreasing) stream of integer readings. Their combined length is guaranteed to be odd, so the merged multiset has a single middle element. Print that median value: the element that would sit in the middle if both streams were merged into one sorted list.
Either stream may be empty (but not both, since the total is at least one). Solve it without materializing a full merge in the worst case — a logarithmic-style search on the values or partitions is expected.
Line 1: an integer n, the length of the first stream.
Line 2: n space-separated integers in non-decreasing order (this line is empty if n is 0).
Line 3: an integer m, the length of the second stream.
Line 4: m space-separated integers in non-decreasing order (this line is empty if m is 0).
A single integer: the median of the merged readings.
Example 1
Input
3 1 3 8 2 2 7
Expected
3
Explanation
Merged the readings are 1 2 3 7 8; the middle (3rd of five) value is 3.
Example 2
Input
2 2 4 1 3
Expected
3
Explanation
Merged the readings are 2 3 4; the middle value is 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →