forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
48 lines (42 loc) · 1.42 KB
/
solution.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
import java.util.Scanner;
public class _2dArrayDS {
private static final int MAX = 6; // size of matrix
private static final int OFFSET = 2; // hourglass width
private static int mat[][] = new int[MAX][MAX];
private static int maxHourglass = -63; // initialize to lowest possible sum (-9 x 7)
/** Given a starting index for an hourglass, sets maxHourglass
* @param i row
* @param j column
**/
private static void hourglass(int i, int j){
int temp = 0; // current hourglass sum
// sum top 3 and bottom 3 elements
for(int k = j; k <= j + OFFSET; k++){
temp += mat[i][k];
temp += mat[i + OFFSET][k];
}
// sum middle element
temp += mat[i + 1][j + 1];
if(maxHourglass < temp){
maxHourglass = temp;
}
}
public static void main(String[] args) {
// read inputs
Scanner scan = new Scanner(System.in);
for(int i=0; i < MAX; i++){
for(int j=0; j < MAX; j++){
mat[i][j] = scan.nextInt();
}
}
scan.close();
// find maximum hourglass
for(int i=0; i < MAX - OFFSET; i++){
for(int j=0; j < MAX - OFFSET; j++){
hourglass(i, j);
}
}
// print maximum hourglass
System.out.println(maxHourglass);
}
}