Numbers arrive one at a time. After each insertion you must report the median of all values seen so far.
To keep the output a single unambiguous integer, the median is defined as follows on the current multiset sorted in non-decreasing order:
Each reported value is uniquely determined by the values seen so far.
Line 1: an integer n, the number of values in the stream.
Line 2: n space-separated integers given in arrival order.
n lines. Line i is the median (as defined above) after the first i values have been inserted.
Example 1
Input
5 5 2 8 1 9
Expected
5 7 5 7 5
Explanation
After 5: median 5. After 5,2 (sorted 2,5): even count, sum of middles 2+5=7. After 5,2,8 (2,5,8): odd, middle 5. After 5,2,8,1 (1,2,5,8): even, 2+5=7. After 5,2,8,1,9 (1,2,5,8,9): odd, middle 5. Output: 5, 7, 5, 7, 5.
Example 2
Input
4 10 20 30 40
Expected
10 30 20 50
Explanation
After 10: 10. After 10,20: even, 10+20=30. After 10,20,30: odd, middle 20. After 10,20,30,40 (sorted): even, 20+30=50. Output: 10, 30, 20, 50.
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 →