Convert Min Heap to Max Heap

Last Updated : 12 May, 2026

Given an array representation of min Heap, convert it to max Heap.

Examples: 

Input: arr[] = [3, 5, 9, 6, 8, 20, 10, 12, 18, 9]

2056957991


Output: arr[] = [20, 18, 10, 12, 9, 9, 3, 5, 6, 8]

2056957992


Input: arr[] = [3, 4, 8, 11, 13]
Output:  arr[] = [13, 11, 8, 4, 3] 

Using Bottom-Up Heapify – O(n) Time and O(log n) Space

The idea is to heapify operations from the last internal node up to the root. Since leaf nodes already satisfy heap property, we only need to fix internal nodes. By heapifying in bottom-up order, every subtree becomes a valid Max Heap, resulting in the complete array forming a Max Heap efficiently.

  • Start from the last non-leaf node of the heap
  • Apply MaxHeapify on each internal node while moving towards the root
  • During heapify, compare node with its children and swap with the larger child if needed
  • Repeat recursively until Max Heap property is satisfied for all nodes
C++
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// to heapify a subtree with root at given index
void maxHeapify(vector<int>& arr, int i)
{
    int n = arr.size();

    int l = 2 * i + 1;
    int r = 2 * i + 2;
    int largest = i;

    if (l < n && arr[l] > arr[i])
        largest = l;

    if (r < n && arr[r] > arr[largest])
        largest = r;

    if (largest!= i) {
        swap(arr[i], arr[largest]);
        maxHeapify(arr, largest);
    }
}

// This function basically builds max heap
void convertMaxHeap(vector<int>& arr)
{
    int n = arr.size();

    // Start from bottommost and rightmost
    // internal node and heapify all internal
    // nodes in bottom up way
    for (int i = (n - 2) / 2; i >= 0; --i)
        maxHeapify(arr, i);
}

// Driver's code
int main()
{
    // array representing Min Heap
    vector<int> arr = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};

    convertMaxHeap(arr);

    for (int i = 0; i < arr.size(); ++i)
        cout << arr[i] << " ";

    return 0;
}
Java
import java.util.*;

class GFG {

    // to heapify a subtree with root at given index
    static void maxHeapify(int[] arr, int i)
    {
        int n = arr.length;

        int l = 2 * i + 1;
        int r = 2 * i + 2;
        int largest = i;

        if (l < n && arr[l] > arr[i])
            largest = l;

        if (r < n && arr[r] > arr[largest])
            largest = r;

        if (largest!= i) {

            int temp = arr[i];
            arr[i] = arr[largest];
            arr[largest] = temp;

            maxHeapify(arr, largest);
        }
    }

    // This function basically builds max heap
    static void convertMaxHeap(int[] arr)
    {
        int n = arr.length;

        // Start from bottommost and rightmost
        // internal node and heapify all internal
        // nodes in bottom up way
        for (int i = (n - 2) / 2; i >= 0; --i)
            maxHeapify(arr, i);
    }

    // Driver's code
    public static void main(String[] args)
    {
        // array representing Min Heap
        int[] arr = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};

        convertMaxHeap(arr);

        for (int i = 0; i < arr.length; ++i)
            System.out.print(arr[i] + " ");
    }
}

// Contributed by Pramod Kumar
Python
# to heapify a subtree with root at given index
def maxHeapify(arr, i):

    n = len(arr)

    l = 2 * i + 1
    r = 2 * i + 2
    largest = i

    if l < n and arr[l] > arr[i]:
        largest = l

    if r < n and arr[r] > arr[largest]:
        largest = r

    if largest!= i:

        arr[i], arr[largest] = arr[largest], arr[i]

        maxHeapify(arr, largest)


# This function basically builds max heap
def convertMaxHeap(arr):

    n = len(arr)

    # Start from bottommost and rightmost
    # internal node and heapify all internal
    # nodes in bottom up way
    for i in range((n - 2) // 2, -1, -1):
        maxHeapify(arr, i)


# Driver's code

# array representing Min Heap
arr = [3, 5, 9, 6, 8, 20, 10, 12, 18, 9]

convertMaxHeap(arr)

for i in range(len(arr)):
    print(arr[i], end=" ")
C#
using System;

class GFG {

    // to heapify a subtree with root at given index
    static void maxHeapify(int[] arr, int i)
    {
        int n = arr.Length;

        int l = 2 * i + 1;
        int r = 2 * i + 2;
        int largest = i;

        if (l < n && arr[l] > arr[i])
            largest = l;

        if (r < n && arr[r] > arr[largest])
            largest = r;

        if (largest!= i) {

            int temp = arr[i];
            arr[i] = arr[largest];
            arr[largest] = temp;

            maxHeapify(arr, largest);
        }
    }

    // This function basically builds max heap
    static void convertMaxHeap(int[] arr)
    {
        int n = arr.Length;

        // Start from bottommost and rightmost
        // internal node and heapify all internal
        // nodes in bottom up way
        for (int i = (n - 2) / 2; i >= 0; --i)
            maxHeapify(arr, i);
    }

    // Driver's code
    static void Main()
    {
        // array representing Min Heap
        int[] arr = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};

        convertMaxHeap(arr);

        for (int i = 0; i < arr.Length; ++i)
            Console.Write(arr[i] + " ");
    }
}
// This code is contributed by nitin mittal.
JavaScript
// to heapify a subtree with root at given index
function maxHeapify(arr, i)
{
    let n = arr.length;

    let l = 2 * i + 1;
    let r = 2 * i + 2;
    let largest = i;

    if (l < n && arr[l] > arr[i])
        largest = l;

    if (r < n && arr[r] > arr[largest])
        largest = r;

    if (largest!= i) {

        [arr[i], arr[largest]] = [arr[largest], arr[i]];

        maxHeapify(arr, largest);
    }
}

// This function basically builds max heap
function convertMaxHeap(arr)
{
    let n = arr.length;

    // Start from bottommost and rightmost
    // internal node and heapify all internal
    // nodes in bottom up way
    for (let i = Math.floor((n - 2) / 2); i >= 0; --i)
        maxHeapify(arr, i);
}

// Driver's code
let arr = [3, 5, 9, 6, 8, 20, 10, 12, 18, 9];
convertMaxHeap(arr);
for (let i = 0; i < arr.length; ++i)
    process.stdout.write(arr[i] + " ");

Output
20 18 10 12 9 9 3 5 6 8 
Comment