 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum element in min heap in C++
Problem statement
Given a minimum heap find maximum element in that.
Example
If input heap is −

Then maximum element is 55
Algorithm
- In minimum heap parent node will be lesser than its children. Hence we can conclude that a non-leaf node cannot be the maximum.
- Search maximum element in the leaf nodes
Example
Let us now see an example −
#include <bits/stdc++.h> using namespace std; int getMaxElement(int *heap, int n) {    int maxVal = heap[n / 2];    for (int i = n / 2 + 1; i < n; ++i) {       maxVal = max(maxVal, heap[i]);    }    return maxVal; } int main() {    int heap[] = {15, 27, 22, 35, 29, 55, 48}; int n = sizeof(heap) / sizeof(heap[0]);    cout << "Maximum element = " << getMaxElement(heap, n) << endl;    return 0; } Output
Maximum element = 55
Advertisements
 