a b c\n d\n e f g<\/code><\/pre>\n\n\n\nFor example, if we create an hourglass using the number 1 within an array full of zeros, it may look like this:<\/p>\n\n\n\n
1 1 1 0 0 0\n 0 1 0 0 0 0\n 1 1 1 0 0 0\n 0 0 0 0 0 0\n 0 0 0 0 0 0\n 0 0 0 0 0 0<\/code><\/pre>\n\n\n\nActually, there are many hourglasses in the array above. The three leftmost hourglasses are the following:<\/p>\n\n\n\n
1 1 1 1 1 0 1 0 0\n 1 0 0\n 1 1 1 1 1 0 1 0 0<\/code><\/pre>\n\n\n\nThe sum of an hourglass is the sum of all the numbers within it. The sum for the hourglasses above are 7, 4, and 2, respectively.<\/p>\n\n\n\n
In this problem you have to print the largest sum among all the hourglasses in the array.<\/p>\n\n\n\n
<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThere will be exactly 6 <\/strong>lines, each containing 6 <\/strong>integers seperated by spaces. Each integer will be between -9 <\/strong>and 9 <\/strong>inclusive.<\/p>\n\n\n\n<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the answer to this problem on a single line.<\/p>\n\n\n\n
<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n 1 1 1 0 0 0\n 0 1 0 0 0 0\n 1 1 1 0 0 0\n 0 0 2 4 4 0\n 0 0 0 2 0 0\n 0 0 1 2 4 0<\/code><\/pre>\n\n\n\n<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n 19<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h4>\n\n\n\nThe hourglass which has the largest sum is:<\/p>\n\n\n\n
2 4 4\n 2\n 1 2 4<\/code><\/pre>\n\n\n\n<\/span>Solution –<\/strong> Java 2D Array<\/strong><\/span><\/h2>\n\n\n\nimport java.io.*;\nimport java.math.*;\nimport java.security.*;\nimport java.text.*;\nimport java.util.*;\nimport java.util.concurrent.*;\nimport java.util.regex.*;\n\npublic class Solution {\n\n \n private static final Scanner scanner = new Scanner(System.in);\n\n public static void main(String[] args) {\n int[][] arr = new int[6][6];\n\n for (int i = 0; i < 6; i++) {\n String[] arrRowItems = scanner.nextLine().split(\" \");\n scanner.skip(\"(\\r\\n|[\\n\\r\\u2028\\u2029\\u0085])?\");\n\n for (int j = 0; j < 6; j++) {\n int arrItem = Integer.parseInt(arrRowItems[j]);\n arr[i][j] = arrItem;\n }\n }\n int csum =Integer.MIN_VALUE;\n for(int i=0;i<6;i++)\n {\n for(int j=0;j<6;j++)\n { \n if(i>1 && j>1)\n {\n int sum=arr[i][j]+arr[i-1][j-1]+arr[i-2][j]+arr[i-2][j-1]\n +arr[i-2][j-2]+arr[i][j-1]+arr[i][j-2];\n \n if(sum>csum)\n {\n csum=sum;\n }\n }\n }\n }\n System.out.print(csum);\n scanner.close();\n }\n}<\/pre>\n\n\n\n