Eroor in Output

programming
Tags: #<Tag:0x00007f242511e090>

#1
int heap[1000001];

int hsize = 0;

void heapifyUp()
{
int i = hsize;
int par = i/2;
int key = heap[hsize];
while(par>0 && heap[par]>key)
{
heap[i] = heap[par];
i = par;
par = i/2;
}
heap[i] = key;
}
void InsertHeap(int k)
{
heap[++hsize] = k;
heapifyUp();
}
void heapifyDown(int i)
{
int smallest = i;
int left = 2*i;
int right = left+1;
if(left<hsize && heap[left]<heap[smallest])
{
smallest = left;
}
if(right<hsize && heap[right]<heap[smallest])
{
smallest = right;
}
if(smallest != i)
{
swap(heap[i],heap[smallest]);
heapifyDown(smallest);
}
return ;
}
int delHeap()
{
int top = heap[1];
heap[1] = heap[hsize];
heapifyDown(1);
hsize–;
return top;
}

int Solution::kthsmallest(const vector &A, int B)
{
int i;
for(i=0;i<A.size();i++)
{
InsertHeap(A[i]);
}
int res=0;
for(i=1;i<=B;i++)
{
res = delHeap();
//cout<<res<<" ";
}
return res;
}