Maximum sum of cocktail glass in a 2D matrix
Last Updated :
09 Jun, 2022
Given a 2D matrix mat[][], the task is to find the maximum sum of a cocktail glass.
A Cocktail glass is made of 6 cells in the following form:
X X
X
X X X
Examples:
Input: mat[][] = {
{1, 0, 4, 0, 0},
{0, 3, 0, 0, 0},
{1, 1, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}}
Output: 11
Below is the cocktail glass with
maximum sum:
1 4
3
1 1 1
Input: mat[][] = {
{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}}
Output: 12
Approach: It is evident from the definition of cocktail glass that the number of rows and number of columns must be greater than or equal to 3. If we count the total number of cocktail glasses in a matrix, we can say that the count is equal to the count of the possible top left cells in a cocktail glass. The number of top-left cells in a cocktail glass is equal to (R – 2) * (C – 2). Therefore, in a matrix total number of cocktail glass is (R – 2) * (C – 2)
For mat[][] = {
{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}}
Possible cocktail glasses are:
0 0 3 6 0 0
1 1 0
1 1 1 1 1 0 1 0 0
0 1 1 0 1 0
1 1 0
0 0 2 0 2 0 2 0 1
1 1 1 0 1 0
0 2 0
0 2 0 2 0 1 0 1 3
We consider all top left cells of cocktail glasses one by one. For every cell, we compute the sum of the cocktail glass formed by it. Finally, we return the maximum sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int R = 5;
const int C = 5;
int findMaxCock( int ar[R][C])
{
if (R < 3 || C < 3)
return -1;
int max_sum = INT_MIN;
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 = max(max_sum, sum);
}
}
return max_sum;
}
int main()
{
int ar[][C] = { { 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 } };
cout << findMaxCock(ar);
return 0;
}
|
Java
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 = Integer.MIN_VALUE;
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 (String[] args)
{
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 } };
System.out.println(findMaxCock(ar));
}
}
|
Python3
import sys
R = 5
C = 5
def findMaxCock(ar):
if (R < 3 or C < 3 ):
return - 1
max_sum = - sys.maxsize - 1
for i in range (R - 2 ):
for j in range (C - 2 ):
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;
if __name__ = = '__main__' :
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 ]]
print (findMaxCock(ar))
|
C#
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));
}
}
|
PHP
<?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 ));
?>
|
Javascript
<script>
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));
</script>
|
Time Complexity : O(R * C)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...