#include <bits/stdc++.h> // Includes common standard libraries (vector, iostream, algorithm for swap)
using namespace std; // Use the standard namespace
// Function to perform heapify operation for a Max Heap.
// It ensures that the subtree rooted at 'index' satisfies the Max Heap property.
// 'arr': The vector representing the heap (passed by reference to allow modification).
// 'index': The 0-based index of the root of the subtree to heapify.
// 'size': The current logical size of the heap (number of valid elements in 'arr').
// Time Complexity: O(log N) where N is the current 'size' of the heap.
// Space Complexity: O(H) for recursion stack (where H is the height of the subtree).
void heapify(vector<int> &arr, int index, int size) {
int largest = index; // Assume current node 'index' is the largest.
int left = 2 * index + 1; // 0-based index of left child.
int right = 2 * index + 2; // 0-based index of right child.
// Check if left child exists within bounds and its value is greater than arr[largest].
if(left < size && arr[largest] < arr[left]) {
largest = left; // If so, left child is the new candidate for largest.
// Check if right child exists within bounds and its value is greater than arr[largest].
// (Note: 'largest' might have been updated to 'left' in the previous step).
if(right < size && arr[largest] < arr[right]) {
largest = right; // If so, right child is the new candidate for largest.
// If 'largest' is not the original 'index', it means a child was larger.
swap(arr[index], arr[largest]); // Swap current node with the largest child.
heapify(arr, largest, size); // Recursively call heapify on the affected subtree
// to ensure property is maintained downwards.
// Function to merge two arrays (or heaps) into a single Max Heap.
// It does this by concatenating the arrays and then building a Max Heap from the result.
// Time Complexity: O(N + M) where N = arr1.size() and M = arr2.size().
// Space Complexity: O(N + M) for the new combined vector.
vector<int> mergeHeaps(vector<int> arr1, vector<int> arr2) {
// Step 1: Concatenate arr2 into arr1.
// This effectively puts all elements into a single vector.
// Step 2: Build a Max Heap from the combined array (arr1).
// Iterate from the last non-leaf node down to the root (index 0).
// For 0-based indexing, non-leaf nodes are from (size/2 - 1) to 0.
// This bottom-up approach is the most efficient way to build a heap (O(N) time).
for(int i = arr1.size() / 2 - 1; i >= 0; i--) {
heapify(arr1, i, arr1.size()); // Apply heapify to each non-leaf node.
// Return the newly formed Max Heap.
// Input elements for the first array.
cout << "Enter the elements of first array (enter -1 to stop) : ";
// Input elements for the second array.
cout << "Enter the elements of second array (enter -1 to stop) : ";
// Call the mergeHeaps function to get the combined Max Heap.
vector<int> result = mergeHeaps(arr1, arr2);
// Print the elements of the merged heap.
cout << "Merged heap : ";
for(int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
Input 1: 42 37 35 25 29 21 -1
Combined Array initially: [42, 37, 35, 25, 29, 21, 93, 34, 65, 11] (size = 10)
Building Max Heap from combined array (i = (10/2)-1 = 4 down to 0):
i = 4 (value 29): Children 2*4+1=9 (11), 2*4+2=10(out). Largest 29. No change.
i = 3 (value 25): Children 2*3+1=7 (34), 2*3+2=8 (65). Largest 65. Swap 25, 65.
Array: [42, 37, 35, 65, 29, 21, 93, 34, 25, 11]
Heapify(arr, 8, 10) (on new 25): Children 2*8+1=17(out). No change.
i = 2 (value 35): Children 2*2+1=5 (21), 2*2+2=6 (93). Largest 93. Swap 35, 93.
Array: [42, 37, 93, 65, 29, 21, 35, 34, 25, 11]
Heapify(arr, 6, 10) (on new 35): Children 2*6+1=13(out). No change.
i = 1 (value 37): Children 2*1+1=3 (65), 2*1+2=4 (29). Largest 65. Swap 37, 65.
Array: [42, 65, 93, 37, 29, 21, 35, 34, 25, 11]
Heapify(arr, 3, 10) (on new 37): Children 2*3+1=7 (34), 2*3+2=8 (25). Largest 37. No change.
i = 0 (value 42): Children 2*0+1=1 (65), 2*0+2=2 (93). Largest 93. Swap 42, 93.
Array: [93, 65, 42, 37, 29, 21, 35, 34, 25, 11]
Heapify(arr, 2, 10) (on new 42): Children 2*2+1=5 (21), 2*2+2=6 (35). Largest 42. No change.
Merged Heap (Example Output): [93 65 42 37 29 21 35 34 25 11] (Order may vary slightly but will be a valid Max Heap)