
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- K’th Least Element in a Min-Heap in C++
- Convert min Heap to max Heap in C++
- Convert BST to Min Heap in C++
- C++ Program to Implement Min Heap
- Minimum element in a max heap in C++.
- Print all nodes less than a value x in a Min Heap in C++
- K-th Greatest Element in a Max-Heap in C++
- Program to find maximum subarray min-product in Python
- Removing the Min Element from Deaps
- Removing the Min Element from Interval Heaps
- Maximum element in tuple list in Python
- Consecutive element maximum product in Python
- How to find the min/max element of an Array in JavaScript?
- Maximum size of a element in HTML
- Find maximum element of HashSet in Java

Advertisements