Maximum sum of cocktail glass in a 2D matrix | C # 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
using System ; class GFG { static int R = 5 ; static int C = 5 ; static int findMaxCock ( int [ , ] ar ) { if ( R < 3 C < 3 ) return - 1 ; int max_sum = int . MinValue ; for ( int i = 0 ; i < R - 2 ; i ++ ) { for ( int j = 0 ; j < C - 2 ; j ++ ) { int 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 ; } public static void Main ( ) { int [ , ] 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 } } ; Console . WriteLine ( findMaxCock ( ar ) ) ; } }