SDE Path

Check if an Array Represents a Max Heap

Easy

Check if an Array Represents a Max Heap

Verify whether a given array satisfies the max-heap property.

Input format

The first line contains N. The second line contains N space-separated integers.

Output format

Print true if the array represents a max heap (a[i] ≥ children for all i), otherwise false.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
5
5 3 4 1 2
Expected output
true
Example 2
Input
4
1 2 3 4
Expected output
false