Temple Offerings | Returns minimum offerings required ; Go through all templs 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
< ? php function offeringNumber ( $ n , $ templeHeight ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ left = 0 ; $ right = 0 ; for ( $ j = $ i - 1 ; $ j >= 0 ; -- $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j + 1 ] ) ++ $ left ; else break ; } for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { if ( $ templeHeight [ $ j ] < $ templeHeight [ $ j - 1 ] ) ++ $ right ; else break ; } $ sum += max ( $ right , $ left ) + 1 ; } return $ sum ; } $ arr1 = array ( 1 , 2 , 2 ) ; echo offeringNumber ( 3 , $ arr1 ) , " STRNEWLINE " ; $ arr2 = array ( 1 , 4 , 3 , 6 , 2 , 1 ) ; echo offeringNumber ( 6 , $ arr2 ) , " STRNEWLINE " ; ? >