-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHourglassSolution.java
More file actions
50 lines (39 loc) · 889 Bytes
/
HourglassSolution.java
File metadata and controls
50 lines (39 loc) · 889 Bytes
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
// Java program to find maximum
// sum of hour glass in matrix
import java.io.*;
public class Solution {
static int Row = 4;
static int Column = 4;
static int findMaxSum(int [][]mat)
{
if (Row < 3 || Column < 3){
System.out.println("Not possible");
System.exit(0);
}
int max_sum = Integer.MIN_VALUE;
for (int i = 0; i < Row - 2; i++)
{
for (int j = 0; j < Column - 2; j++)
{
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]);
max_sum = Math.max(max_sum, sum);
}
}
return max_sum;
}
// Driver code
static public void main (String[] args)
{
int [][]mat = {{1, 2, 3, 4},
{5,6,7,8},
{9,10,1,2},
{3,4,5,6}};
int res = findMaxSum(mat);
System.out.println("Maximum sum of hour glass = "+ res);
}
}
/**
* @author Pradumn Patel */