You are viewing a single comment's thread. Return to all comments →
import java.util.Scanner;
public class HourglassSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
int[][] arr = new int[6][6]; // Input 6x6 array for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { arr[i][j] = sc.nextInt(); } } int maxSum = Integer.MIN_VALUE; // Traverse possible hourglass centers (i from 1 to 4, j from 1 to 4) for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { int sum = arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1] + // top arr[i][j] + // center arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1]; // bottom if (sum > maxSum) { maxSum = sum; } } } System.out.println(maxSum); sc.close(); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Day 11: 2D Arrays
You are viewing a single comment's thread. Return to all comments →
import java.util.Scanner;
public class HourglassSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
}