We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Your swim function is absolutely fine, but what I dont get is why you swap(heap, parent, pos) everytime in the while loop.
swim()
voidswim(){// finding the min element.intmin_pos=1;for(inti=2;i<=N;++i){if(Heap[i]<Heap[min_pos]){min_pos=i;}}// swapping the min element in the heap with the element at 1st position.inttemp=Heap[1];Heap[1]=Heap[min_pos];Heap[min_pos]=temp;}
my main()
intmain(){intQ;// no of queriescin>>Q;while(Q--){intquery;cin>>query;if(query==1){intnum;cin>>num;Heap[++N]=num;swim();}elseif(query==2){intnum;cin>>num;remove_heap(num);build_minheap();}elseif(query==3){cout<<Heap[1]<<endl;}}
let me know if you see some scope for improvement; :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
QHEAP1
You are viewing a single comment's thread. Return to all comments →
Your swim function is absolutely fine, but what I dont get is why you swap(heap, parent, pos) everytime in the while loop.
swim()
my main()
let me know if you see some scope for improvement; :)