![]() |
VOOZH | about |
Given an array arr[] of size N representing integers required to be read as a data stream, the task is to calculate and print the median after reading every integer.
Examples:
Input: arr[] = { 5, 10, 15 } Output: 5 7.5 10 Explanation: After reading arr[0] from the data stream, the median is 5. After reading arr[1] from the data stream, the median is 7.5. After reading arr[2] from the data stream, the median is 10.
Input: arr[] = { 1, 2, 3, 4 } Output: 1 1.5 2 2.5
Approach: The problem can be solved using Ordered Set. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
1 1.5 2 2.5 3
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)