Suppose you know a priori the number of elements in the stream.

**Solution:**

given a running average, you can update the group by adding a new element and removing the oldest one. This will give you a solution that is linear in the number of elements in the stream

Similar intuition is for the median, you can use a balanced BST where every node has the number of nodes contained in the subtree rooted at it.

BST would be costly to consider due to balancing.

ReplyDeleteRather we can make use of one median element and two heaps, a MinHeap and a MaxHeap. As the elements from stream are read, push the median to appropriate heap and update the median with balanced element.

We published a sample implementation of above approach http://www.geeksforgeeks.org/archives/14873

ReplyDelete