0% found this document useful (0 votes)
6 views

static unit 5

Emma collects integers to analyze prime numbers and creates a max heap from them, displaying non-prime numbers with a message. Liam builds a min heap from integers for efficient retrieval of the smallest number and finds the maximum value. Tao manages a min-heap allowing for insertion, removal of the root, and calculations of sum and average of nodes, while Harish merges two sales lists and sorts them using a heap-based sorting algorithm.

Uploaded by

varnikarathi24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

static unit 5

Emma collects integers to analyze prime numbers and creates a max heap from them, displaying non-prime numbers with a message. Liam builds a min heap from integers for efficient retrieval of the smallest number and finds the maximum value. Tao manages a min-heap allowing for insertion, removal of the root, and calculations of sum and average of nodes, while Harish merges two sales lists and sorts them using a heap-based sorting algorithm.

Uploaded by

varnikarathi24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

static

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".

Refer to the sample output for the formatting specifications.

Code constraints :
1 ≤ n ≤ 10

1 ≤ values ≤ 100

Sample test cases :


Input 1 :
5
1 3 2 4 5
Output 1 :
1 is not a prime number
4 is not a prime number
Max Heap: 5 2 3
Input 2 :
6
17 7 5 11 13 19
Output 2 :
Max Heap: 19 13 17 7 11 5

solution #include <stdio.h>


#include <stdbool.h>

void swap(int arr[], int a, int b) {


int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}

void maxHeapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(arr, i, largest);
maxHeapify(arr, n, largest);
}
}

void buildMaxHeap(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--)
maxHeapify(arr, n, i);
}

void insertIntoMaxHeap(int arr[], int *n, int value) {


arr[*n] = value;
int i = *n;
(*n)++;
while (i != 0 && arr[(i - 1) / 2] < arr[i]) {
swap(arr, i, (i - 1) / 2);
i = (i - 1) / 2;
}
}

bool isPrime(int num) {


if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}

void printMaxHeap(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}

int main() {
int arr[100];
int n = 0;
int num_elements;

scanf("%d", &num_elements);

for (int i = 0; i < num_elements; i++) {


int value;
scanf("%d", &value);

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:

Display the elements of the min heap as integers separated by a space.


Display the maximum value among the elements in the min heap in the format:
"Maximum: <value>".

Refer to the sample output for the formatting specifications.

Code constraints :
1 ≤ n ≤ 10

1 ≤ values ≤ 1000

Sample test cases :


Input 1 :
5
3 9 2 6 8
Output 1 :
2 6 3 9 8
Maximum: 9
Input 2 :
8
25 15 17 32 23 5 12 8
Output 2 :
5 8 12 23 25 17 15 32
Maximum: 32

solution #include <iostream>


using namespace std;

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

void minHeapify(int heap[], int size, int i) {


int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < size && heap[left] < heap[smallest])


smallest = left;
if (right < size && heap[right] < heap[smallest])
smallest = right;
if (smallest != i) {
swap(&heap[i], &heap[smallest]);
minHeapify(heap, size, smallest);
}
}

void insertElement(int heap[], int &size, int value, int capacity) {


if (size == capacity) {
return; // Cannot insert if the heap is full
}

size++;
int i = size - 1;
heap[i] = value;

while (i != 0 && heap[(i - 1) / 2] > heap[i]) {


swap(&heap[i], &heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}

void displayMinHeap(int heap[], int size) {


for (int i = 0; i < size; i++) {
cout << heap[i] << " ";
}
cout << endl;
}

int findMaxValue(int heap[], int size) {


int maxValue = heap[0];
for (int i = 1; i < size; i++) {
if (heap[i] > maxValue) {
maxValue = heap[i];
}
}
return maxValue;
}
int main() {
int n;
cin >> n;

int capacity = n;
int heap[capacity];
int size = 0;

for (int i = 0; i < n; i++) {


int value;
cin >> value;
insertElement(heap, size, value, capacity);
}

displayMinHeap(heap, size);

int maxValue = findMaxValue(heap, size);


cout << "Maximum: " << maxValue << endl;

return 0;
}

3.Tao is developing a program to manage a min-heap data structure for a small


application. The program should enable Tao to insert elements, remove the root
node, find the maximum value in the heap, and calculate the sum and average of all
nodes in the heap.

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.

Refer to the sample output for the formatting specifications.

Code constraints :
1 ≤ n ≤ 10

1 ≤ values ≤ 10

Sample test cases :


Input 1 :
5
6 4 3 7 8
Output 1 :
Min Heap: 3 6 4 7 8
Root node: 3
Maximum value: 8
Sum of nodes: 25
Average of nodes: 6.25
Input 2 :
5
3 9 2 6 8
Output 2 :
Min Heap: 2 6 3 9 8
Root node: 2
Maximum value: 9
Sum of nodes: 26
Average of nodes: 6.50

solution ..// You are using GCC


#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

// Helper function to swap two elements


void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

// Function to maintain the min-heap property by heapifying the subtree rooted at


index 'i'
void minHeapify(vector<int> &heap, int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

// 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 insert an element into the heap


void insert(vector<int> &heap, int value) {
heap.push_back(value);
int i = heap.size() - 1;

// Bubble up the newly added element to restore the heap property


while (i != 0 && heap[(i - 1) / 2] > heap[i]) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}

// Function to remove the root node (minimum element) from the heap
void removeRoot(vector<int> &heap) {
int n = heap.size();
if (n == 0) return;

// Replace root with the last element


swap(heap[0], heap[n - 1]);
heap.pop_back();

// Heapify the root to restore the min-heap property


minHeapify(heap, heap.size(), 0);
}

// Function to print the heap


void printHeap(const vector<int> &heap) {
cout << "Min Heap: ";
for (int i = 0; i < heap.size(); i++) {
cout << heap[i] << " ";
}
cout << endl;
}

// Function to find the maximum value in the heap


int findMax(const vector<int> &heap) {
return *max_element(heap.begin(), heap.end());
}

// Function to calculate the sum of all nodes in the heap


int calculateSum(const vector<int> &heap) {
int sum = 0;
for (int value : heap) {
sum += value;
}
return sum;
}

// Function to calculate the average of all nodes in the heap


double calculateAverage(const vector<int> &heap) {
int sum = calculateSum(heap);
return static_cast<double>(sum) / heap.size();
}

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);
}

// Print the initial min-heap


printHeap(heap);

// Remove the root node (min element)


int root = heap[0];
removeRoot(heap);

// Print the value of the root node


cout << "Root node: " << root << endl;

// Print the maximum value in the heap


int maxValue = findMax(heap);
cout << "Maximum value: " << maxValue << endl;

// Print the sum of the remaining nodes


int sum = calculateSum(heap);
cout << "Sum of nodes: " << sum << endl;

// Print the average of the remaining nodes


double average = calculateAverage(heap);
cout << "Average of nodes: " << fixed << setprecision(2) << average << endl;

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.

Refer to the sample output for the formatting specifications.


Code constraints :
1 ≤ n1, n2 ≤ 10

1 ≤ sales lists ≤ 100

Sample test cases :


Input 1 :
5
6 4 3 8 7
3
9 5 2
Output 1 :
2 3 4 5 6 7 8 9
Input 2 :
5
3 9 2 6 8
5
17 7 5 10 11
Output 2 :
2 3 5 6 7 8 9 10 11 17

solution .. #include <iostream>


using namespace std;

void swap(int& x, int& y) {


int temp = x;
x = y;
y = temp;
}

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

for (int i = n - 1; i > 0; i--) {


swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

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;

cin >> n1;


int arr1[n1];

for (int i = 0; i < n1; i++) {


cin >> arr1[i];
}

cin >> n2;


int arr2[n2];

for (int i = 0; i < n2; i++) {


cin >> arr2[i];
}

int result[n1 + n2];


mergeArrays(arr1, n1, arr2, n2, result);

heapSort(result, n1 + n2);

for (int i = 0; i < n1 + n2; i++) {


cout << result[i] << " ";
}
cout << endl;

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.

The second line contains n space-separated integers representing the 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.

Refer to the sample output for the formatting specifications.

Code constraints :
1 ≤ n ≤ 10

1 ≤ ratings ≤ 100

Sample test cases :


Input 1 :
3
9 5 2
Output 1 :
2 5 9
Median: 5.00
Input 2 :
6
17 7 5 10 11 22
Output 2 :
5 7 10 11 17 22
Median: 10.50

solution .. #include <iostream>


#include <iomanip>
using namespace std;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

for (int i = n - 1; i > 0; i--) {


swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

double calculateMedian(int arr[], int n) {


if (n % 2 == 0)
return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
else
return arr[n / 2];
}

int main() {
int n;
cin >> n;

int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

heapSort(arr, n);

double median = calculateMedian(arr, n);

for (int i = 0; i < n; i++) {


cout << arr[i] << " ";
}
cout << endl;

cout << fixed << setprecision(2);


cout << "Median: " << median << endl;

return 0;
}

You might also like