#include <bits/stdc++.h> // Includes common standard libraries (iostream, vector, algorithm for swap)
using namespace std; // Use the standard namespace
Heap Array Representation (0-based indexing):
// Function to perform heapify operation for a Min Heap.
// It ensures that the subtree rooted at 'i' satisfies the Min Heap property.
// 'arr': The vector representing the heap.
// 'n': The current logical size of the heap (number of valid elements).
// 'i': The index of the root of the subtree to heapify.
// Time Complexity: O(log N)
// Space Complexity: O(log N) for recursion stack.
void heapify(vector<int>& arr, int n, int i) {
int smallest = i; // Assume current node 'i' is the smallest.
int left = 2 * i + 1; // Index of left child (0-based).
int right = 2 * i + 2; // Index of right child (0-based).
// Check if left child exists within bounds and is smaller than current 'smallest'.
if(left < n && arr[smallest] > arr[left]) {
smallest = left; // If so, left child is the new candidate for smallest.
// Check if right child exists within bounds and is smaller than current 'smallest'.
// (Note: 'smallest' might now be 'left' if left child was smaller).
if(right < n && arr[smallest] > arr[right]) {
smallest = right; // If so, right child is the new candidate for smallest.
// If 'smallest' is not the original 'i', it means a child was smaller.
swap(arr[i], arr[smallest]); // Swap current node with the smallest child.
heapify(arr, n, smallest); // Recursively call heapify on the affected subtree
// to ensure property is maintained downwards.
// Function to build a Min Heap from an arbitrary vector.
// Time Complexity: O(N) (optimized for heap construction)
// Space Complexity: O(log N) due to recursive heapify calls.
vector<int> buildMinHeap(vector<int>& arr) {
int n = arr.size(); // Get the total number of elements in the array.
// Iterate backwards from the last non-leaf node up to the root (index 0).
// For 0-based indexing, the last non-leaf node is at (n/2) - 1.
// Leaves (from n/2 to n-1) are already considered trivial heaps.
for(int i = (n / 2) - 1; i >= 0; i--) {
heapify(arr, n, i); // Call heapify for each non-leaf node.
return arr; // Return the heapified array.
// Example array to be converted into a Min Heap.
vector<int> arr = {90, 30, 20, 120, 50, 60, 40, 150};
// Print the array before building the Min Heap.
cout << "Before building Min Heap : ";
for(int i = 0; i < arr.size(); i++) {
// Call buildMinHeap to transform the array into a Min Heap.
// Print the array after building the Min Heap.
// The smallest element (20) should be at index 0.
cout << "After building Min Heap : ";
for(int i = 0; i < arr.size(); i++) {
Example Input Array: {90, 30, 20, 120, 50, 60, 40, 150} (n = 8)
[90, 30, 20, 120, 50, 60, 40, 150]
Non-leaf nodes indices for n=8:
(8/2) - 1 = 3. So, we iterate i from 3 down to 0.
Children: 2*3+1 = 7 (150)
120 is smaller than 150. Heapify(3) does nothing.
Array: [90, 30, 20, 120, 50, 60, 40, 150]
Children: 2*2+1 = 5 (60), 2*2+2 = 6 (40)
Smallest among (20, 60, 40) is 20. Heapify(2) does nothing.
Array: [90, 30, 20, 120, 50, 60, 40, 150]
Children: 2*1+1 = 3 (120), 2*1+2 = 4 (50)
Smallest among (30, 120, 50) is 30. Heapify(1) does nothing.
Array: [90, 30, 20, 120, 50, 60, 40, 150]
Children: 2*0+1 = 1 (30), 2*0+2 = 2 (20)
Smallest among (90, 30, 20) is 20.
Swap 90 and 20. Array: [20, 30, 90, 120, 50, 60, 40, 150]
Recursively call heapify(arr, 8, 2) (on new position of 90)
Node at 2 is 90. Children: 2*2+1 = 5 (60), 2*2+2 = 6 (40)
Smallest among (90, 60, 40) is 40.
Swap 90 and 40. Array: [20, 30, 40, 120, 50, 60, 90, 150]
Recursively call heapify(arr, 8, 6) (on new position of 90)
Node at 6 is 90. Children: 2*6+1 = 13 (out of bounds), 2*6+2 = 14 (out of bounds)
Heapify(6) does nothing. Returns.
Final Min Heap Array (approximate, exact order depends on tie-breaking rules and build process):
[20, 30, 40, 120, 50, 60, 90, 150]
This is a valid Min Heap.