-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTwoDArrays.java
More file actions
26 lines (22 loc) Β· 809 Bytes
/
TwoDArrays.java
File metadata and controls
26 lines (22 loc) Β· 809 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
package arrays;
import helper.Input;
public class TwoDArrays {
public static void main(String[] args) {
int[][] matrix = Input.getMatrix(6, 6);
System.out.println(maxHourGlassSum(matrix));
}
private static int maxHourGlassSum(int[][] data) {
int result = Integer.MIN_VALUE;
for (int row = 0 ; row < 4 ; row++) {
for (int column = 0 ; column < 4 ; column++) {
result = Math.max(result, hourGlassSum(data, row, column));
}
}
return result;
}
private static int hourGlassSum(int[][] data, int row, int column) {
int sum = 0;
for (int index = column ; index < column + 3 ; sum += data[row][index] + data[row+2][index], index++);
return sum + data[row + 1][column + 1];
}
}