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.
t u v
x
x y z
Examples:
Input : mat[][] = [[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
Explanation: Below is the hour glass with maximum sum:
1 1 1
1
1 1 1Input : mat[][] = [[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
Explanation: Below is the hour glass with maximum sum:
1 0 0
4
0 2 4
Approach:
The idea is to traverse through the entire matrix and check each possible position where a complete hourglass pattern is possible.
If we count the total number of hourglasses in a matrix of size R x C, 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
Step by step approach:
- Check if the matrix dimensions are at least 3Ã3 to fit an hourglass. If not, return -1.
- Iterate through the matrix, stopping 3 rows and 3 columns before the end.
- At each position, sum the 7 cells forming the hourglass pattern (top row, middle element, bottom row).
- Compare the current sum with the maximum sum found so far and update if needed.
// C++ program to find Maximum
// sum of hour glass in matrix
#include <bits/stdc++.h>
using namespace std;
int maxHourGlassSum(vector<vector<int>> &mat) {
int n = mat.size(), m = mat[0].size();
// If matrix is too small
// to contain an hourglass
if (n < 3 || m < 3)
return -1;
int res = INT_MIN;
for (int i = 0; i <= n - 3; i++) {
for (int j = 0; j <= m - 3; j++) {
// Calculate sum of current hourglass
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];
// Update result if current hourglass sum is greater
res = max(res, sum);
}
}
return res;
}
int main() {
vector<vector<int>> mat = {
{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}
};
cout << maxHourGlassSum(mat);
return 0;
}
// Java program to find Maximum
// sum of hour glass in matrix
class GfG {
static int maxHourGlassSum(int[][] mat) {
int n = mat.length, m = mat[0].length;
// If matrix is too small
// to contain an hourglass
if (n < 3 || m < 3)
return -1;
int res = Integer.MIN_VALUE;
for (int i = 0; i <= n - 3; i++) {
for (int j = 0; j <= m - 3; j++) {
// Calculate sum of current hourglass
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];
// Update result if current hourglass sum is greater
res = Math.max(res, sum);
}
}
return res;
}
public static void main(String[] args) {
int[][] mat = {
{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}
};
System.out.println(maxHourGlassSum(mat));
}
}
# Python program to find Maximum
# sum of hour glass in matrix
def maxHourGlassSum(mat):
n = len(mat)
m = len(mat[0])
# If matrix is too small
# to contain an hourglass
if n < 3 or m < 3:
return -1
res = float('-inf')
for i in range(n - 2):
for j in range(m - 2):
# Calculate sum of current hourglass
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]
# Update result if current hourglass sum is greater
res = max(res, sum)
return res
if __name__ == "__main__":
mat = [
[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]
]
print(maxHourGlassSum(mat))
// C# program to find Maximum
// sum of hour glass in matrix
using System;
class GfG {
static int maxHourGlassSum(int[][] mat) {
int n = mat.Length, m = mat[0].Length;
// If matrix is too small
// to contain an hourglass
if (n < 3 || m < 3)
return -1;
int res = int.MinValue;
for (int i = 0; i <= n - 3; i++) {
for (int j = 0; j <= m - 3; j++) {
// Calculate sum of current hourglass
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];
// Update result if current hourglass sum is greater
res = Math.Max(res, sum);
}
}
return res;
}
static void Main(string[] args) {
int[][] mat = new int[][] {
new int[] {1, 1, 1, 0, 0},
new int[] {0, 1, 0, 0, 0},
new int[] {1, 1, 1, 0, 0},
new int[] {0, 0, 0, 0, 0},
new int[] {0, 0, 0, 0, 0}
};
Console.WriteLine(maxHourGlassSum(mat));
}
}
// JavaScript program to find Maximum
// sum of hour glass in matrix
function maxHourGlassSum(mat) {
let n = mat.length, m = mat[0].length;
// If matrix is too small
// to contain an hourglass
if (n < 3 || m < 3)
return -1;
let res = Number.MIN_SAFE_INTEGER;
for (let i = 0; i <= n - 3; i++) {
for (let j = 0; j <= m - 3; j++) {
// Calculate sum of current hourglass
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];
// Update result if current hourglass sum is greater
res = Math.max(res, sum);
}
}
return res;
}
const mat = [
[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]
];
console.log(maxHourGlassSum(mat));
Output
7
Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(1)