Maximum sum of hour glass in matrix
Last Updated :
31 Jul, 2022
Given a 2D matrix, the task is to find the maximum sum of an hourglass.
An hour glass is made of 7 cells
in following form.
A B C
D
E F G
Examples:
Input : 1 1 1 0 0
0 1 0 0 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0
Output : 7
Below is the hour glass with
maximum sum:
1 1 1
1
1 1 1
Input : 0 3 0 0 0
0 1 0 0 0
1 1 1 0 0
0 0 2 4 4
0 0 0 2 4
Output : 11
Below is the hour glass with
maximum sum
1 0 0
4
0 2 4
Approach:
It is evident from the definition of the hourglass that the number of rows and number of columns must be equal to 3. If we count the total number of hourglasses in a matrix, we can say that the count is equal to the count of possible top left cells in an hourglass. The number of top-left cells in an hourglass is equal to (R-2)*(C-2). Therefore, in a matrix total number of an hourglass is (R-2)*(C-2).
mat[][] = 2 3 0 0 0
0 1 0 0 0
1 1 1 0 0
0 0 2 4 4
0 0 0 2 0
Possible hour glass are :
2 3 0 3 0 0 0 0 0
1 0 0
1 1 1 1 1 0 1 0 0
0 1 0 1 0 0 0 0 0
1 1 0
0 0 2 0 2 4 2 4 4
1 1 1 1 1 0 1 0 0
0 2 4
0 0 0 0 0 2 0 2 0
Consider all top left cells of hourglasses one by one. For every cell, we compute the sum of the hourglass formed by it. Finally, return the maximum sum.
Below is the implementation of the above idea:
C++
#include<bits/stdc++.h>
using namespace std;
const int R = 5;
const int C = 5;
int findMaxSum( int mat[R][C])
{
if (R<3 || C<3){
cout << "Not possible" << endl;
exit (0);
}
int max_sum = INT_MIN;
for ( int i=0; i<R-2; i++)
{
for ( int j=0; j<C-2; j++)
{
int sum = (mat[i][j]+mat[i][j+1]+mat[i][j+2])+
(mat[i+1][j+1])+
(mat[i+2][j]+mat[i+2][j+1]+mat[i+2][j+2]);
max_sum = max(max_sum, sum);
}
}
return max_sum;
}
int main()
{
int mat[][C] = {{1, 2, 3, 0, 0},
{0, 0, 0, 0, 0},
{2, 1, 4, 0, 0},
{0, 0, 0, 0, 0},
{1, 1, 0, 1, 0}};
int res = findMaxSum(mat);
cout << "Maximum sum of hour glass = " << res << endl;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#define R 5
#define C 5
int MaxSum( int arr[R][C])
{
int i, j, sum;
if (R<3 || C<3){
printf ( "Not Possible" );
exit (0);
}
int max_sum
= -500000;
for (i = 0; i < R - 2; i++) {
for (j = 0; j < C - 2; j++) {
sum = (arr[i][j] + arr[i][j + 1]
+ arr[i][j + 2])
+ (arr[i + 1][j + 1])
+ (arr[i + 2][j] + arr[i + 2][j + 1]
+ arr[i + 2][j + 2]);
if (sum > max_sum)
max_sum = sum;
else
continue ;
}
}
return max_sum;
}
int main()
{
int arr[][C] = { { 1, 2, 3, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 2, 1, 4, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 1, 0 } };
int res = MaxSum(arr);
printf ( "Maximum sum of hour glass = %d" , res);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int R = 5 ;
static int C = 5 ;
static int findMaxSum( int [][]mat)
{
if (R < 3 || C < 3 ){
System.out.println( "Not possible" );
System.exit( 0 );
}
int max_sum = Integer.MIN_VALUE;
for ( int i = 0 ; i < R - 2 ; i++)
{
for ( int j = 0 ; j < C - 2 ; j++)
{
int sum = (mat[i][j] + mat[i][j + 1 ] +
mat[i][j + 2 ]) + (mat[i + 1 ][j + 1 ]) +
(mat[i + 2 ][j] + mat[i + 2 ][j + 1 ] +
mat[i + 2 ][j + 2 ]);
max_sum = Math.max(max_sum, sum);
}
}
return max_sum;
}
static public void main (String[] args)
{
int [][]mat = {{ 1 , 2 , 3 , 0 , 0 },
{ 0 , 0 , 0 , 0 , 0 },
{ 2 , 1 , 4 , 0 , 0 },
{ 0 , 0 , 0 , 0 , 0 },
{ 1 , 1 , 0 , 1 , 0 }};
int res = findMaxSum(mat);
System.out.println( "Maximum sum of hour glass = " + res);
}
}
|
Python3
R = 5
C = 5
def MaxSum(arr):
max_sum = - 50000
if (R < 3 or C < 3 ):
print ( "Not possible" )
exit()
for i in range ( 0 , R - 2 ):
for j in range ( 0 , C - 2 ):
SUM = (arr[i][j] + arr[i][j + 1 ] + arr[i][j + 2 ]) + (arr[i + 1 ][j + 1 ]) + (arr[i + 2 ][j] +
arr[i + 2 ][j + 1 ] + arr[i + 2 ][j + 2 ])
if ( SUM > max_sum):
max_sum = SUM
else :
continue
return max_sum
arr = [[ 1 , 2 , 3 , 0 , 0 ],
[ 0 , 0 , 0 , 0 , 0 ],
[ 2 , 1 , 4 , 0 , 0 ],
[ 0 , 0 , 0 , 0 , 0 ],
[ 1 , 1 , 0 , 1 , 0 ]]
res = MaxSum(arr)
print (f "Maximum sum of hour glass = {res}" )
|
C#
using System;
class GFG {
static int R = 5;
static int C = 5;
static int findMaxSum( int [,]mat)
{
if (R < 3 || C < 3){
Console.WriteLine( "Not possible" );
Environment.Exit(0);
}
int max_sum = int .MinValue;
for ( int i = 0; i < R - 2; i++)
{
for ( int j = 0; j < C - 2; j++)
{
int sum = (mat[i, j] + mat[i, j + 1] +
mat[i, j + 2]) + (mat[i + 1, j + 1]) +
(mat[i + 2, j] + mat[i + 2, j + 1] +
mat[i + 2, j + 2]);
max_sum = Math.Max(max_sum, sum);
}
}
return max_sum;
}
static public void Main(String[] args)
{
int [,]mat = {{1, 2, 3, 0, 0},
{0, 0, 0, 0, 0},
{2, 1, 4, 0, 0},
{0, 0, 0, 0, 0},
{1, 1, 0, 1, 0}};
int res = findMaxSum(mat);
Console.WriteLine( "Maximum sum of hour glass = " + res);
}
}
|
PHP
<?php
$R = 5;
$C = 5;
function findMaxSum( $mat )
{
global $R ; global $C ;
if ( $R < 3 || $C < 3){
exit ( "Not possible" );
}
$max_sum = PHP_INT_MIN;
for ( $i = 0; $i < ( $R - 2); $i ++)
{
for ( $j = 0; $j < ( $C - 2); $j ++)
{
$sum = ( $mat [ $i ][ $j ] + $mat [ $i ][ $j + 1] +
$mat [ $i ][ $j + 2]) +
( $mat [ $i + 1][ $j + 1]) +
( $mat [ $i + 2][ $j ] +
$mat [ $i + 2][ $j + 1] +
$mat [ $i + 2][ $j + 2]);
$max_sum = max( $max_sum , $sum );
}
}
return $max_sum ;
}
$mat = array ( array (1, 2, 3, 0, 0),
array (0, 0, 0, 0, 0),
array (2, 1, 4, 0, 0),
array (0, 0, 0, 0, 0),
array (1, 1, 0, 1, 0));
$res = findMaxSum( $mat );
echo "Maximum sum of hour glass = " , $res , "\n" ;
?>
|
Javascript
<script>
let R = 5;
let C = 5;
function findMaxSum(mat)
{
if (R < 3 || C < 3){
document.write( "Not possible" );
process.exit(0);
}
let max_sum = Number.MIN_VALUE;
for (let i = 0; i < R - 2; i++)
{
for (let j = 0; j < C - 2; j++)
{
let sum = (mat[i][j] + mat[i][j + 1] +
mat[i][j + 2]) + (mat[i + 1][j + 1]) +
(mat[i + 2][j] + mat[i + 2][j + 1] +
mat[i + 2][j + 2]);
max_sum = Math.max(max_sum, sum);
}
}
return max_sum;
}
let mat = [[1, 2, 3, 0, 0],
[0, 0, 0, 0, 0],
[2, 1, 4, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 0, 1, 0]];
let res = findMaxSum(mat);
document.write( "Maximum sum of hour glass = " + res);
</script>
|
Output
Maximum sum of hour glass = 13
Time complexity: O(R x C).
Auxiliary Space: O(1)
Reference :
http://stackoverflow.com/questions/38019861/hourglass-sum-in-2d-array
Share your thoughts in the comments
Please Login to comment...