How do i use heap to solve this problem?

If i cosntruct a heap from the given inputs, it would have two branches at every level; then how would i decide which path to take?

@tusharnitharwal i dont understand why would there be 2 branches at every level? In a heap, you only have to worry about the top element. Remember it is not a BST, it is a priority queue. If you want to get to the kth element, you will have to first pop k-1 elements.

suppose there is a min heap with values 1 2 3 10 11 7 8 here if k is 4 then answer should be 7 but if i pop k-1 elements i will get 10 which would be wrong

sorry my bad. Got it

1 Like

@tusharnitharwal please mark your doubt as resolved