Maximum sum of cocktail glass in a 2D matrix | PHP implementation of the approach ; Function to return the maximum sum of a cocktail glass ; If no cocktail glass is possible ; Initialize max_sum with the mini ; Here loop runs ( R - 2 ) * ( C - 2 ) times considering different top left cells of cocktail glasses ; Considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; Update the max_sum ; Driver code
< ? php $ R = 5 ; $ C = 5 ; function findMaxCock ( $ ar ) { global $ R , $ C ; if ( $ R < 3 $ C < 3 ) return -1 ; $ max_sum = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ R - 2 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ C - 2 ; $ j ++ ) { $ sum = ( $ ar [ $ i ] [ $ j ] + $ ar [ $ i ] [ $ j + 2 ] ) + ( $ ar [ $ i + 1 ] [ $ j + 1 ] ) + ( $ ar [ $ i + 2 ] [ $ j ] + $ ar [ $ i + 2 ] [ $ j + 1 ] + $ ar [ $ i + 2 ] [ $ j + 2 ] ) ; $ max_sum = max ( $ max_sum , $ sum ) ; } } return $ max_sum ; } $ ar = array ( array ( 0 , 3 , 0 , 6 , 0 ) , array ( 0 , 1 , 1 , 0 , 0 ) , array ( 1 , 1 , 1 , 0 , 0 ) , array ( 0 , 0 , 2 , 0 , 1 ) , array ( 0 , 2 , 0 , 1 , 3 ) ) ; echo ( findMaxCock ( $ ar ) ) ; ? >