How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; driver code
function isHeap ( arr , n ) { for ( let i = 0 ; i <= Math . floor ( ( n - 2 ) / 2 ) ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false ; if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) return false ; } return true ; } let arr = [ 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ] ; let n = arr . length ; if ( isHeap ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }