Instagram
youtube
Facebook
Twitter

Find the Running Median| Heap

Task(Hard)

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then:
• If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set {1, 2, 3}, 2 is the median. A
• If your set contains an even number of elements, the median is the average of the two middle elements of the sorted sample. In the sorted set {1, 2, 3, 4), 2+ = 2.5 is the median.
Given an input stream of n integers, perform the following task for each th integer:
1. Add the integer to a running list of integers.
2. Find the median of the updated list (i.e., for the first element through the sth element).
3. Print the updated median on a new line. The printed value must be a double-precision number scaled to 1 decimal place (i.e., 12.3 format).
Example a = (7,3,5,2)
Sorted
[7]
[3, 7]
[3, 5, 7]
[2, 3, 5, 7]

Median
7.0
5.0
5.0
4.0

Each of the median values is stored in an array and the array is returned for the main function to print.
Note: Add formatting to the print statement.

Function Description
Complete the runningMedian function in the editor below.
runningMedian has the following parameters:
- int a[n]: an array of integers

Returns
- float[n]: the median of the array after each insertion, modify the print statement in main to get proper formatting. A

Input Format
The first line contains a single integer, n, the number of integers in the data stream. Each line i of the n subsequent lines contains an integer, a[i], to be inserted into the list.

Constraints

• 1 ≤ n ≤105
• 0 ≤ a[i] ≤ 105


Sample Input

STDIN
6


Function
 

12
4
5
a[] size n = 6
a[12, 4, 5, 3, 8, 7]


Submissions

3
8
7


Sample Output

12.0
8.0
5.0
4.5
5.0
6.0

Explanation
There are n = 6 integers, so we must print the new median on a new line as each integer is added to the list:
1. list = {12), median = 12.0
2. list = {4, 12), median = 1+12 = 8.0
3. list = (4, 5, 12), median = 5.0
4. list = {3, 4, 5, 12), median = + = 4.5
5. list = (3,4,5,8, 12), median = 5.0
6. list = (3, 4, 5, 7, 8, 12), median = 1 = 6.0

SOLUTION 1

def runningMedian(a):

   lower_half = []   

upper_half = []   

result = []

for num in a:

    if len(lower_half) == 0 or num <= -lower_half[0]:

        heapq.heappush(lower_half, -num)  

    else:

        heapq.heappush(upper_half, num)

    if len(lower_half) > len(upper_half) + 1:

        moved_item = -heapq.heappop(lower_half)

        heapq.heappush(upper_half, moved_item)

    elif len(upper_half) > len(lower_half):   

        moved_item = heapq.heappop(upper_half)

        heapq.heappush(lower_half, -moved_item)

    if len(lower_half) == len(upper_half):

        median = (-lower_half[0] + upper_half[0]) / 2.0

    else:

        median = -lower_half[0] / 1.0  

    result.append(format(median, ".1f"))

return result