What is a Max Heap?
A Max Heap is a special type of binary tree that satisfies two conditions:
Complete Binary Tree
Heap Property (Max Heap Rule)
In simple words:
The largest element is always at the root, and every parent is bigger than its children.
How is a Heap Stored in an Array?
Instead of using nodes and pointers, heaps are usually stored in arrays.
For any index i:
Left child → 2*i + 1
Right child → 2*i + 2
Parent → (i - 1) / 2
Example
Valid Max Heap
Array: [90, 15, 10, 7, 12, 2]
Tree form:
90
/ \
15 10
/ \ /
7 12 2
Every parent is greater than children → Valid Max Heap
Invalid Max Heap
Array: [9, 15, 10, 7, 12, 11]
Tree form:
9
/ \
15 10
/ \ \
7 12 11
9 < 15 → Violates max heap property
Key Observation
You only need to check parent nodes, not all elements.
Why?
Because leaf nodes have no children, so they always satisfy the condition.
Algorithm
Loop from i = 0 to (n/2) - 1
For each index:
Check left child
Check right child
If any child is greater than parent → return false
If all checks pass → return true
Java Implementation
class Solution {
public boolean isMaxHeap(int[] arr) {
int n = arr.length;
for (int i = 0; i <= (n / 2) - 1; i++) {
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[i] < arr[left]) {
return false;
}
if (right < n && arr[i] < arr[right]) {
return false;
}
}
return true;
}
}
Complexity Analysis
Why This Works Efficiently
We avoid building a tree → saves time and space
Direct index math makes checking fast
Only half the array is processed
Common Mistakes to Avoid
Checking all nodes instead of only non-leaf nodes
Forgetting boundary checks (left < n, right < n)
Confusing min heap with max heap
Final Insight
Think of it like this:
“If every parent node dominates its children, the structure is a max heap.”