-
Notifications
You must be signed in to change notification settings - Fork 3
/
Java2dArray.java
39 lines (34 loc) Β· 1.24 KB
/
Java2dArray.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
// https://www.hackerrank.com/challenges/java-2d-array/problem
import java.util.Scanner;
public class Java2dArray {
public static void main(String[] args) {
int[][] matrix = getInput();
System.out.println(maxHourGlassSum(matrix));
}
private static int maxHourGlassSum(int[][] matrix) {
int sum = Integer.MIN_VALUE;
for (int row = 0 ; row < 4 ; row++) {
for (int column = 0 ; column < 4 ; column++) {
sum = Math.max(sum, hourGlassSum(row, column, matrix));
}
}
return sum;
}
private static int hourGlassSum(int row, int column, int[][] matrix) {
int sum = 0;
for (int index = 0 ; index < 3 ; sum += matrix[row][column + index++]);
for (int index = 0 ; index < 3 ; sum += matrix[row + 2][column + index++]);
return sum + matrix[row + 1][column + 1];
}
private static int[][] getInput() {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[6][6];
for (int row = 0 ; row < 6 ; row++) {
for (int column = 0 ; column < 6 ; column++) {
matrix[row][column] = scanner.nextInt();
}
}
scanner.close();
return matrix;
}
}