I\'m trying to come up with something to solve the following:
Given a max-heap represented as an array, return the kth largest element without modifyi
Max-heap in an array: element at i is larger than elements at 2*i+1 and 2*i+2 (i is 0-based)
You'll need another max heap (insert, pop, empty) with element pairs (value, index) sorted by value. Pseudocode (without boundary checks):
input: k
1. insert (at(0), 0)
2. (v, i) <- pop and k <- k - 1
3. if k == 0 return v
4. insert (at(2*i+1), 2*i+1) and insert (at(2*+2), 2*+2)
5. goto 2
Runtime evaluation