-
Notifications
You must be signed in to change notification settings - Fork 0
/
25_maximum-sum-of-hour-glass.java
68 lines (59 loc) · 2.06 KB
/
25_maximum-sum-of-hour-glass.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 04-2024(April)/25_maximum-sum-of-hour-glass.java
/**
* Date : 25-Apr-24
* Repo : https://github.com/ankitsamaddar/daily-geeksforgeeks
*
* Problem : Maximum sum of hour glass
* Difficulty: 🟡Medium
*
* GeeksforGeeks : https://www.geeksforgeeks.org/problems/maximum-sum-of-hour-glass3842/1
*/
import java.io.*;
// User function Template for Java
class Solution {
// SIMULATION APPROACH
int findMaxSum(int n, int m, int[][] mat) {
// Initialize the maximum hourglass sum as the smallest possible integer
int maxSum = Integer.MIN_VALUE;
// Traverse the given matrix
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < m - 2; j++) {
// Calculate the sum of the current hourglass
int currentSum = mat[i][j] + mat[i][j + 1] + mat[i][j + 2] + // top part
mat[i + 1][j + 1] + // middle part
mat[i + 2][j] + mat[i + 2][j + 1] + mat[i + 2][j + 2]; // bottom part
// Update the maximum hourglass sum
maxSum = Math.max(maxSum, currentSum);
}
}
// If the maximum hourglass sum is still the smallest possible integer, return -1
if (maxSum == Integer.MIN_VALUE) {
return -1;
}
// Return the maximum hourglass sum
return maxSum;
}
}
//{ Driver Code Starts
// Initial Template for Java
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));
}
}
}
// } Driver Code Ends