Median of Two Sorted Arrays
Kirkwall Health runs two clinics that each report waiting times, already listed shortest first. A regulator wants the middle waiting time across both clinics together — the value with as many readings above it as below. When the two clinics have an even number of readings between them there is no single middle value, and the regulator's rule is to take the halfway point of the two innermost readings. One clinic may have reported nothing at all on a given day.
Task: Print the middle value of the two lines taken together.
Input
Two lines. Each holds up to 1000 integers separated by single spaces, already in increasing order. Either line may be empty, but not both. The two lines may be different lengths.
Output
One line holding the middle value of all the numbers from both lines combined. With an odd count of readings that is the single middle one. With an even count it is the halfway point between the two innermost readings. Print it as a plain integer when it comes out whole, with no decimal point and no trailing zero; otherwise print it with its one decimal place.
Example:
Input:
1 3
2
Output:
2
Second example:
Input:
1 2
3 4
Output:
2.5
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in