static unit 5
static unit 5
1Emma is a mathematics enthusiast who wants to analyze prime numbers for her
research. She collects several integers and aims to create a max heap that only
includes the prime numbers from her collection.
After inserting the prime numbers into the max heap, she wants to visualize the max
heap structure to understand the hierarchy of these numbers.
Note
A prime number is a natural number greater than one that has no positive divisors
other than one and itself.
Input format :
The first line contains an integer n, representing the number of integers Emma
collected.
The second line consists of n space-separated integers, which are the values Emma
wants to analyze.
Output format :
The output consists of two parts:
If any prime numbers were inserted into the max heap, display them as integers
separated by a space.
If a number is not prime, print a message in the format: "<value> is not a prime
number".
Code constraints :
1 ≤ n ≤ 10
1 ≤ values ≤ 100
int main() {
int arr[100];
int n = 0;
int num_elements;
scanf("%d", &num_elements);
if (isPrime(value)) {
insertIntoMaxHeap(arr, &n, value);
} else {
printf("%d is not a prime number\n", value);
}
}
if (n > 0) {
buildMaxHeap(arr, n);
printf("Max Heap: ");
printMaxHeap(arr, n);
}
return 0;
}
2. Liam is a data analyst who is working on optimizing a list of numbers for
further analysis. He decides to create a min heap from the integers he collects, as
it allows him to efficiently retrieve the smallest number at any time.
After inserting the numbers into the min heap, he wants to visualize its structure
and also find the maximum value among the elements.
Input format :
The first line contains an integer n, representing the number of integers Liam
collected.
The second line consists of n space-separated integers, which are the values Liam
wants to insert into the min heap.
Output format :
The output consists of two parts:
Code constraints :
1 ≤ n ≤ 10
1 ≤ values ≤ 1000
size++;
int i = size - 1;
heap[i] = value;
int capacity = n;
int heap[capacity];
int size = 0;
displayMinHeap(heap, size);
return 0;
}
The program must ensure that the min-heap property is maintained throughout these
operations.
Input format :
The first line contains an integer n, representing the number of elements to be
inserted into the min-heap.
The second line consists of n space-separated integers, representing which are the
values to be inserted into the heap.
Output format :
The output displays the following format:
Print the elements of the min-heap in order, separated by spaces, on a single line.
Print the value of the root node after removing it.
Print the maximum value present in the heap after the removal operation.
Print the sum of all remaining nodes in the heap.
Print the average of all remaining nodes in the heap, rounded to two decimal
places.
Code constraints :
1 ≤ n ≤ 10
1 ≤ values ≤ 10
// Check if left child exists and is smaller than the current smallest
if (left < n && heap[left] < heap[smallest]) {
smallest = left;
}
// Check if right child exists and is smaller than the current smallest
if (right < n && heap[right] < heap[smallest]) {
smallest = right;
}
// If the smallest is not the current node, swap and recursively heapify
if (smallest != i) {
swap(heap[i], heap[smallest]);
minHeapify(heap, n, smallest);
}
}
// Function to remove the root node (minimum element) from the heap
void removeRoot(vector<int> &heap) {
int n = heap.size();
if (n == 0) return;
int main() {
int n;
cin >> n;
vector<int> heap;
// Input elements to be inserted into the heap
for (int i = 0; i < n; i++) {
int value;
cin >> value;
insert(heap, value);
}
return 0;
}
4. Harish has two lists of integers, one representing the number of items sold in
two different months. He wants to merge these two lists and sort the combined data
to understand overall sales trends. Harish decides to use a sorting algorithm that
structures the data into a heap to simplify the sorting process.
Your task is to help Harish by merging the two lists and then sorting the combined
list in ascending order. Finally, display the sorted list of sales for Harish.
Input format :
The first line contains an integer n1, representing the number of items sold in the
first month.
The second line contains n1 space-separated integers representing the sales data
for the first month.
The third line contains an integer n2, representing the number of items sold in the
second month.
The fourth line contains n2 space-separated integers representing the sales data
for the second month.
Output format :
The output prints a single line containing the combined and sorted sales data as
space-separated integers.
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int result[]) {
for (int i = 0; i < n1; i++) {
result[i] = arr1[i];
}
for (int i = 0; i < n2; i++) {
result[n1 + i] = arr2[i];
}
}
int main() {
int n1, n2;
heapSort(result, n1 + n2);
return 0;
}
5. Maya is conducting a survey to analyze customer feedback ratings for a new
product. She collects ratings from a number of customers and wants to sort these
ratings using the heap sort algorithm to identify trends. Additionally, Maya is
interested in calculating the median rating to understand the central tendency of
customer satisfaction.
Your task is to help Maya sort the ratings in ascending order using heap sort and
then calculate and display the median value. Finally, present both the sorted
ratings and the median in a clear format.
Input format :
The first line contains an integer n, representing the number of customer ratings.
Output format :
The first line prints a single line containing the sorted ratings as space-
separated integers.
The second line prints a second line displaying the median rating formatted to two
decimal places as a double value.
Code constraints :
1 ≤ n ≤ 10
1 ≤ ratings ≤ 100
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
heapSort(arr, n);
return 0;
}