Maximum element in min heap

28 May, 2021
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++
Java
Python3
C#
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to find the// maximum element in a// min heapint findMaximumElement(int heap[], int n){    int maximumElement = heap[0];     for (int i = 1; i < n; ++i)        maximumElement = max(maximumElement, heap[i]);     return maximumElement;} // Driver codeint 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 implementation of above approachclass 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 implementation of above approach # Function to find the maximum element# in a min heapdef findMaximumElement(heap, n):     maximumElement = heap[0];     for i in range(1, n):            maximumElement = max(maximumElement, heap[i]);     return maximumElement; # Driver codeif __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# implementation of above approachusing System; class GFG{// Function to find the maximum element// in a min heapstatic 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 codepublic 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
<script> // JavaScript implementation of above approach // Function to find the maximum element// in a min heap     function findMaximumElement(heap , n) {        var maximumElement = heap[0];         for (i = 1; i < n; ++i) {            maximumElement = Math.max(maximumElement,                    heap[i]);        }         return maximumElement;    } // Driver code             // Number of nodes        var n = 10;         // heap represents the following min heap:        // 10        // / \        // 25 23        // / \ / \        // 45 50 30 35        // / \ /        //63 65 81        var heap = [10, 25, 23, 45, 50,            30, 35, 63, 65, 81];         document.write(findMaximumElement(heap, n));  // This code contributed by aashish1995 </script>
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 is 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++14
Java
Python 3
C#
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to find the// maximumelement in a// max heapint 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 codeint 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 implementation of above approachimport java.util.*;import java.lang.*;import java.io.*; class GFG{      // Function to find the// maximumelement in a// max heapstatic 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 codepublic 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 implementation of# above approach # Function to find the maximum# element in a max heapdef findMaximumElement(heap, n):         maximumElement = heap[n // 2]         for i in range(1 + n // 2, n):        maximumElement = max(maximumElement,                             heap[i])    return maximumElement # Driver Coden = 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# implementation of above approachusing System; class GFG{ // Function to find the// maximumelement in a// max heapstatic 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 codepublic 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
<script>// javascript implementation of above approach    // Function to find the    // maximumelement in a    // max heap    function findMaximumElement(heap , n) {        var maximumElement = heap[n / 2];         for (i = 1 + n / 2; i < n; ++i)            maximumElement = Math.max(maximumElement, heap[i]);         return maximumElement;    }     // Driver code             // Number of nodes        var n = 10;         // heap represents the following min heap:        // 10        // / \        // 25 23        // / \ / \        // 45 50 30 35        // / \ /        // 63 65 81        var heap = [ 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 ];         document.write(findMaximumElement(heap, n)); // This code contributed by aashish1995</script>
Output: 
81 
tufan_gupta2000
joshiyogesh
Akanksha_Rai
princiraj1992
princi singh
trusted
fabrizzioorderique
aashish1995
Technical Scripter 2018
Heap
Technical Scripter
Heap
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Sliding Window Maximum (Maximum of all subarrays of size k)
Building Heap from Array
Max Heap in Java
Priority Queue in Python
Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
Insertion and Deletion in Heaps
Sort a nearly sorted (or K sorted) array
Merge k sorted arrays | Set 1
Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()