Temple Offerings | Returns minimum offerings required ; Go through all temples one by one ; Go to left while height keeps increasing ; Go to right while height keeps increasing ; This temple should offer maximum of two values to follow the rule . ; Driver code
function offeringNumber ( n , templeHeight ) { for ( let i = 0 ; i < n ; ++ i ) { let left = 0 , right = 0 ; for ( let j = i - 1 ; j >= 0 ; -- j ) { if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) ++ left ; else break ; } for ( let j = i + 1 ; j < n ; ++ j ) { if ( templeHeight [ j ] < templeHeight [ j - 1 ] ) ++ right ; else break ; } sum += Math . max ( right , left ) + 1 ; } return sum ; } let arr1 = [ 1 , 2 , 2 ] ; document . write ( offeringNumber ( 3 , arr1 ) + " " ) ; let arr2 = [ 1 , 4 , 3 , 6 , 2 , 1 ] ; document . write ( offeringNumber ( 6 , arr2 ) ) ;