Given a min heap, find the maximum element present in the heap.
Examples:
Input : 10
/ \
25 23
/ \ / \
45 30 50 40
Output : 50
Input : 20
/ \
40 28
Output : 40
Brute force approach:
We can check all the nodes in the min heap to get the maximum element. Note that this approach works on any binary tree and does not makes use of any property of the min heap. It has a time and space complexity of O(n). Since min heap is a complete binary tree, we generally use arrays to store them, so we can check all the nodes by simply traversing the array. If the heap is stored using pointers, then we can use recursion to check all the nodes.
Below is the implementation of above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find the // maximum element in a // min heap int findMaximumElement(int heap[], int n) { int maximumElement = heap[0]; for (int i = 1; i < n; ++i) maximumElement = max(maximumElement, heap[i]); return maximumElement; } // Driver code int main() { // Number of nodes int n = 10; // heap represents the following min heap: // 10 // / \ // 25 23 // / \ / \ // 45 50 30 35 // / \ / //63 65 81 int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 }; cout << findMaximumElement(heap, n); return 0; } |
Java
// Java implementation of above approach class GFG { // Function to find the maximum element // in a min heap static int findMaximumElement(int[] heap, int n) { int maximumElement = heap[0]; for (int i = 1; i < n; ++i) { maximumElement = Math.max(maximumElement, heap[i]); } return maximumElement; } // Driver code public static void main(String[] args) { // Number of nodes int n = 10; // heap represents the following min heap: // 10 // / \ // 25 23 // / \ / \ // 45 50 30 35 // / \ / //63 65 81 int[] heap = {10, 25, 23, 45, 50, 30, 35, 63, 65, 81}; System.out.print(findMaximumElement(heap, n)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of above approach # Function to find the maximum element # in a min heap def findMaximumElement(heap, n): maximumElement = heap[0]; for i in range(1, n): maximumElement = max(maximumElement, heap[i]); return maximumElement; # Driver code if __name__ == '__main__': # Number of nodes n = 10; # heap represents the following min heap: # 10 # / \ # 25 23 # / \ / \ # 45 50 30 35 # / \ / #63 65 81 heap = [ 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 ]; print(findMaximumElement(heap, n)); # This code is contributed by Princi Singh |
C#
// C# implementation of above approach using System; class GFG { // Function to find the maximum element // in a min heap static int findMaximumElement(int[] heap, int n) { int maximumElement = heap[0]; for (int i = 1; i < n; ++i) maximumElement = Math.Max(maximumElement, heap[i]); return maximumElement; } // Driver code public static void Main() { // Number of nodes int n = 10; // heap represents the following min heap: // 10 // / \ // 25 23 // / \ / \ // 45 50 30 35 // / \ / //63 65 81 int[] heap = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 }; Console.Write(findMaximumElement(heap, n)); } } // This code is contributed by Akanksha Rai |
Output:
81
Efficient approach:
The min heap property requires that the parent node be lesser than its child node(s). Due to this, we can conclude that a non-leaf node cannot be the maximum element as its child node has a higher value. So we can narrow down our search space to only leaf nodes. In a min heap having n elements, there are ceil(n/2) leaf nodes. The time and space complexity remains O(n) as a constant factor of 1/2 does not affect the asymptotic complexity.
Below is the implementation of above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find the // maximumelement in a // max heap int findMaximumElement(int heap[], int n) { int maximumElement = heap[n / 2]; for (int i = 1 + n / 2; i < n; ++i) maximumElement = max(maximumElement, heap[i]); return maximumElement; } // Driver code int main() { // Number of nodes int n = 10; // heap represents the following min heap: // 10 // / \ // 25 23 // / \ / \ // 45 50 30 35 // / \ / //63 65 81 int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 }; cout << findMaximumElement(heap, n); return 0; } |
Java
// Java implementation of above approach import java.util.*; import java.lang.*; import java.io.*; class GFG{ // Function to find the // maximumelement in a // max heap static int findMaximumElement(int heap[], int n) { int maximumElement = heap[n / 2]; for (int i = 1 + n / 2; i < n; ++i) maximumElement = Math.max(maximumElement, heap[i]); return maximumElement; } // Driver code public static void main(String args[]) { // Number of nodes int n = 10; // heap represents the following min heap: // 10 // / \ // 25 23 // / \ / \ // 45 50 30 35 // / \ / //63 65 81 int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 }; System.out.println(findMaximumElement(heap, n)); } } |
Python 3
# Python 3 implementation of # above approach # Function to find the maximum # element in a max heap def findMaximumElement(heap, n): maximumElement = heap[n // 2] for i in range(1 + n // 2, n): maximumElement = max(maximumElement, heap[i]) return maximumElement # Driver Code n = 10 # Numbers Of Node # heap represents the following min heap: # 10 # / \ # 25 23 # / \ / \ # 45 50 30 35 # / \ \ #63 65 81 heap = [10, 25, 23, 45, 50, 30, 35, 63, 65, 81] print(findMaximumElement(heap, n)) # This code is contributed by Yogesh Joshi |
C#
// C# implementation of above approach using System; class GFG { // Function to find the // maximumelement in a // max heap static int findMaximumElement(int[] heap, int n) { int maximumElement = heap[n / 2]; for (int i = 1 + n / 2; i < n; ++i) maximumElement = Math.Max(maximumElement, heap[i]); return maximumElement; } // Driver code public static void Main() { // Number of nodes int n = 10; // heap represents the following min heap: // 10 // / \ // 25 23 // / \ / \ // 45 50 30 35 // / \ / //63 65 81 int[] heap = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 }; Console.WriteLine(findMaximumElement(heap, n)); } } // This code is contributed // by Akanksha Rai |
Output:
81
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- K’th Least Element in a Min-Heap
- k largest(or smallest) elements in an array | added Min Heap method
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert BST to Min Heap
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Print all nodes less than a value x in a Min Heap.
- Fibonacci Heap - Deletion, Extract min and Decrease key
- Maximise the number of toys that can be purchased with amount K using min Heap
- Merge two sorted arrays in constant space using Min Heap
- Min Heap in Python
- Complexity analysis of various operations of Binary Min Heap
- Minimum element in a max heap
- K-th Greatest Element in a Max-Heap
- Applications of Heap Data Structure
- Tournament Tree (Winner Tree) and Binary Heap
- Time Complexity of building a heap
- Binomial Heap
- Why is Binary Heap Preferred over BST for Priority Queue?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

