codeChallenge/HourGlass.java
2024-10-06 16:55:45 -04:00

35 lines
838 B
Java

public class HourGlass {
/*
* 1 1 1 0 0 0
* 0 1 0 0 0 0
* 1 1 1 0 0 0
* 0 0 0 0 0 0
* 0 0 0 0 0 0
* 0 0 0 0 0 0
*/
public static void main(String[] args) {
int[][] ar = { { 1, 1, 1, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0 }, { 0, 0, 2, 4, 4, 0 },
{ 0, 0, 0, 2, 0, 0 }, { 0, 0, 1, 2, 4, 0 } };
System.out.println(sumInt(ar));
}
private static int sumInt(int[][] arr) {
int rows = arr.length;
int columns = arr[0].length;
int total = -63;
for (int i = 0; i < rows - 2; i++) {
for (int j = 0; j < columns - 2; j++) {
int currentSum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j]
+ arr[i + 2][j + 1] + arr[i + 2][j + 2];
total = Math.max(total, currentSum);
}
}
return total;
}
}