You are viewing a single comment's thread. Return to all comments →
import java.util.*;
public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] arr = new int[6][6];
// Reading 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; // To handle negative numbers too // Loop through possible hourglass centers for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { int sum = arr[i][j] // middle + arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1] // top + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1]; // bottom maxSum = Math.max(maxSum, sum); } } System.out.println(maxSum); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Java 2D Array
You are viewing a single comment's thread. Return to all comments →
import java.util.*;
public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] arr = new int[6][6];
}