Initialize two heaps: a max-heap lo and a min-heap hi.
Initialize a hash table to keep track of invalid elements.
For each window:
a. Insert the incoming element into the appropriate heap.
b. Remove the outgoing element from the heap by marking it in the hash table.
c. Balance the heaps by moving elements between them if necessary.
d. Remove the top elements of the heaps if they are marked as invalid in the hash table.
e. Calculate the median based on the tops of the heaps.