heap#7
#include <iostream>
#include <stdlib.h>
using namespace std;
void minHeapify(int heap[], int n, int i) {
int sml=i;
int ls = 2i+1;
int rs = 2i+2;
if(ls<n && heap[ls]<heap[sml]) sml = ls;
if(rs<n && heap[rs]<heap[sml]) sml = rs;
if(sml != i){
swap(heap[i],heap[sml]);
minHeapify(heap,n,sml);
}
}
void buildMinHeap(int heap[], int n) {
for(int i=n/2-1;i>=0;i--){
minHeapify(heap,n,i);
}
}
void insertElement(int heap[], int* n, int value) {
if(*n>=10){
cout<<"Heap is Full";
return;
}
heap[*n] = value;
(*n)++;
buildMinHeap(heap,*n);
}
void displayMinHeap(int heap[], int size) {
for(int i=0;i<size;i++){
cout<<heap[i]<<" ";
}cout<<endl;
}
int main() {
int n;
cin >> n;
int *heap = (int *)malloc(n * sizeof(int));
int size = 0;
for (int i = 0; i < n; i++) {
int value;
cin >> value;
if (value % 5 == 0) {
insertElement(heap, &size, value);
} else {
cout << value << " is not a multiple of five" << endl;
}
}
buildMinHeap(heap, size);
displayMinHeap(heap, size);
free(heap);
return 0;
}