## Wednesday, May 25, 2016

### K’th largest element in a stream

http://www.geeksforgeeks.org/kth-largest-element-in-a-stream
Given an infinite stream of integers, find the k’th largest element at any point of time.
Example:
```Input:
stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}
k = 3

Output:    {_,   _, 10, 11, 20, 40, 50,  50, ...}```
Extra space allowed is O(k).

`void` `kthLargest(``int` `k)`
`{`
`    ``// count is total no. of elements in stream seen so far`
`    ``int` `count = 0, x;  ``// x is for new element`
`    ``// Create a min heap of size k`
`    ``int` `*arr = ``new` `int``[k];`
`    ``MinHeap mh(arr, k);`
`    ``while` `(1)`
`    ``{`
`        ``// Take next element from stream`
`        ``cout << ``"Enter next element of stream "``;`
`        ``cin >> x;`
`        ``// Nothing much to do for first k-1 elements`
`        ``if` `(count < k-1)`
`        ``{`
`            ``arr[count] = x;`
`            ``count++;`
`        ``}`
`        ``else`
`        ``{`
`          ``// If this is k'th element, then store it`
`          ``// and build the heap created above`
`          ``if` `(count == k-1)`
`          ``{`
`              ``arr[count] = x;`
`              ``mh.buildHeap();`
`          ``}`
`          ``// If next element is greater than  k'th`
`          ``// largest, then replace the root`
`          ``if` `(x > mh.getMin())`
`            ``mh.replaceMin(x); ``// replaceMin calls heapify()`
`          ``// Root of heap is k'th largest element`
`          ``cout << ``"K'th largest element is "`
`               ``<< mh.getMin() << endl;`
`          ``count++;`
`        ``}`
`    ``}`
`}`
http://prpds.blogspot.com/2011/07/kth-largest-from-infinite-stream.html

Extend:
https://discuss.leetcode.com/topic/115/uber-find-the-largest-kth-element-in-a-data-stream
Suppose you have a system, the input is an dynamic integer data stream. Suppose the data stream contains duplicate value. Find the kth largest elements in the data stream and return the index. If the duplicate values exist in multiple index, return them all.
Eg: The data stream is 4,2,2,3,1, k is 4, the answer is 2. The index is 1,2.
The data steam is 1,2,3,4,5, k is 3, the answer is 3 and the index is 2.

We can use  MinHeap + hashatable,  minheap to maintain k largest element, use hashtable to store the count. when element is removed from heap, it's also removed from the hashtable. Space is O(K).

Still remember some genius' code was to store pair<int value, int idx> in a heap, then keep drawing them out. reply