VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-min-heap-to-max-heap/

⇱ Convert Min Heap to Max Heap - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Output
20 18 10 12 9 9 3 5 6 8 
Comment
Article Tags:
Article Tags: