Skip to content

Commit

Permalink
Create Maximum sum of hour glass
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Apr 25, 2024
1 parent d177c1a commit eae03a2
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Maximum sum of hour glass
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//Maximum sum of hour glass

import java.io.*;
import java.util.*;

class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());

while(t-- > 0) {
String s[] = read.readLine().split(" ");
int N = Integer.parseInt(s[0]);
int M = Integer.parseInt(s[1]);
int Mat[][] = new int[N][M];

for(int i = 0; i < N; i++) {
String S[] = read.readLine().split(" ");

for(int j = 0; j < M; j++) {
Mat[i][j] = Integer.parseInt(S[j]);
}
}
Solution ob = new Solution();
System.out.println(ob.findMaxSum(N, M, Mat));
}
}
}

class Solution {
int findMaxSum(int n, int m, int mat[][]) {
if(n < 3 || m < 3) {
return -1;
}

int ans = Integer.MIN_VALUE;

for(int i = 2; i < n; i++) {
int sum = 0;

for(int j = 1; j < m - 1; j++) {
sum = mat[i][j - 1] + mat[i - 2][j - 1] + mat[i - 1][j] + mat[i][j] + mat[i - 2][j]
+ mat[i][j + 1] + mat[i - 2][j + 1];
ans = Math.max(ans, sum);
}
}

return ans;
}
};

0 comments on commit eae03a2

Please sign in to comment.