← DiffPush

Check if an Array Represents a Max Heap

Baseline Heaps · Learning O(n) · O(1) iterative / O(log n) recursion depth

The Parking Garage Priority Audit

A garage stores vehicles in an array layout where slot i's children sit at fixed positions 2i+1 and 2i+2, and the garage rules demand that every occupied parent outrank its children. After reshuffling, an auditor must verify the layout still honors the hierarchy — walking each parent and comparing it against whichever children actually exist.

Input: An integer array A representing the layout.

Output: Return true if A satisfies the binary max-heap property, false otherwise.

Constraints

Examples

Example 1

Input: {"A":[90,15,10,7,12,2]}
Output: true
Every parent (90, 15, 10) is strictly greater than its existing children.

Example 2

Input: {"A":[9,15,10,7,12,11]}
Output: false
The root 9 is smaller than its child 15, violating the hierarchy at the first slot.

Solve this in your browser →

Also on LeetCode ↗