Group E

Q22: Read the marks obtained by students of second year in an online examination of a particular subject. Find out maximum and minimum marks obtained in that subject. Use heap data structure. Analyze the algorithm.

Maximum and Minimum Marks using Heap

Solution and implementation for Q22 from Data Structures and Algorithms Laboratory (dsal).

22_max_min_marks_heap.cpp Download
#include <iostream>
#include <vector>
using namespace std;

// Function to insert into Min Heap
void minInsert(vector<int>& heap, int i) {
    int current = i;
    int parent = (current - 1) / 2;

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

// Function to insert into Max Heap
void maxInsert(vector<int>& heap, int i) {
    int current = i;
    int parent = (current - 1) / 2;

    while (parent >= 0 && heap[parent] < heap[current]) {
        swap(heap[parent], heap[current]);
        current = parent;
        parent = (current - 1) / 2;
    }
}

int main() {
    int n;
    cout << "Enter the number of students: ";
    cin >> n;

    vector<int> marks(n);
    cout << "Enter the marks obtained by the students:\n";
    for (int i = 0; i < n; i++) {
        cin >> marks[i];
    }

    // Build Min Heap
    vector<int> minHeap = marks;
    for (int i = 1; i < n; i++) {
        minInsert(minHeap, i);
    }

    // Build Max Heap
    vector<int> maxHeap = marks;
    for (int i = 1; i < n; i++) {
        maxInsert(maxHeap, i);
    }

    cout << "Minimum marks obtained: " << minHeap[0] << endl;
    cout << "Maximum marks obtained: " << maxHeap[0] << endl;

    return 0;
}

Other Questions in Data Structures and Algorithms Laboratory

See All Available Questions
Download