The next problem is based on 2D Arrays.Arrays are very important concept in Java.

Quick knowledge test question for you all, Arrays are classified as primitive data type, object or a Class ? I would have loved to tell you the answer but it would be not the right approach. Google it and let us also know the answer in comments sections…

**Task **

Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.

**Sample Input**

1 2 3 4 5 6 |
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 |

**Sample Output**

1 |
12 |

So here the Solution you have landed here for..

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 |
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int arr[][] = new int[6][6]; for(int i=0; i < 6; i++){ for(int j=0; j < 6; j++){ arr[i][j] = in.nextInt(); } } int sum[] = new int[16]; int h = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { sum[h] = 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]; h++; } } Arrays.sort(sum); System.out.println(sum[15]); } } |

Thanks for the visit checkout the day 12 tutorial as well.. 🙂