Question: Create a java code that will take user input and find the median using two heaps (max and min heap). The two heap must sort

Create a java code that will take user input and find the median using two heaps (max and min heap). The two heap must sort the elements from small to large. Running time must be efficient and below O(n^2). The algorithm must support the following methods:

- insert(b): insert a given element b in O(logn) time

- getMedian(): return the median in O(1) time

- removeMedian(): remove and return the median in O(logn) time

Note: The median will return the element that exist in the list, for example an even number of elements <9, 9, 1, 2> will return 2 instead of 5 which doesn't exist in the list.

Odd list exp <4, 8, 2> will return 4 (sorting <2,4,8>). Another even exp <15, -2, 10, -5, 11, 18, 5, 1> will return 5 (sorting <-5, -2, 1, 5, 10, 11, 15, 18>)

Answer with java or pseudo code and analyse the running time.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!