Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hour glass problem #780

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Hour glass problem
m-soumik committed Oct 27, 2021
commit 2b3d7f5ac9d0df8409b9dad0974599938541d291
48 changes: 48 additions & 0 deletions Maximum sum of hour glass in matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<bits/stdc++.h>
using namespace std;
const int R = 5;
const int C = 5;

// Returns maximum sum of hour glass in ar[][]
int findMaxSum(int mat[R][C])
{
if (R<3 || C<3)
return -1;

// Here loop runs (R-2)*(C-2) times considering
// different top left cells of hour glasses.
int max_sum = INT_MIN;
for (int i=0; i<R-2; i++)
{
for (int j=0; j<C-2; j++)
{
// Considering mat[i][j] as top left cell of
// hour glass.
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]);

// If previous sum is less then current sum then
// update new sum in max_sum
max_sum = max(max_sum, sum);
}
}
return max_sum;
}

// Driver code
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);
if (res == -1)
cout << "Not possible" << endl;
else
cout << "Maximum sum of hour glass = "
<< res << endl;
return 0;
}