Maximum sum of cocktail glass in a 2D matrix | Javascript 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
var R = 5 ; var C = 5 ; function findMaxCock ( ar ) { if ( R < 3 C < 3 ) return - 1 ; var max_sum = - 1000000000 ; for ( var i = 0 ; i < R - 2 ; i ++ ) { for ( var j = 0 ; j < C - 2 ; j ++ ) { var 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 = Math . max ( max_sum , sum ) ; } } return max_sum ; } ar = [ [ 0 , 3 , 0 , 6 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] , [ 0 , 0 , 2 , 0 , 1 ] , [ 0 , 2 , 0 , 1 , 3 ] ] ; document . write ( findMaxCock ( ar ) ) ;