-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum sum of hour glass
50 lines (40 loc) · 1.37 KB
/
Maximum sum of hour glass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}
};