HackerRank Java 2D Array problem solution YASH PAL, 31 July 202416 January 2026 HackerRank Java 2D Array problem solution – In this HackerRank Java 2D Array problem in Java programming, you have to print the largest sum among all the hourglasses in the array.You are given a 6*6 2D array. An hourglass in an array is a portion shaped like this: a b c d e f g For example, if we create an hourglass using the number 1 within an array full of zeros, it may look like this: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 Actually, there are many hourglasses in the array above. The three leftmost hourglasses are the following:1 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 1 0 1 0 0 The 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. In this problem you have to print the largest sum among all the hourglasses in the array.HackerRank Java 2D Array problem solution.import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { int a[][] = new int[6][6]; int maxSum = Integer.MIN_VALUE; try (Scanner scanner = new Scanner(System.in);) { for(int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { a[i][j] = scanner.nextInt(); if (i > 1 && j > 1) { int sum = a[i][j] + a[i][j-1] + a[i][j-2] + a[i-1][j-1] + a[i-2][j] + a[i-2][j-1] + a[i-2][j-2]; if (sum > maxSum) {maxSum = sum;} } } } } System.out.println(maxSum); } }Second solutionimport 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 sc=new Scanner(System.in); int N[][]=new int[6][6]; for(int i=0; i<6; i++){ for(int j=0; j<6; j++){ N[i][j]=sc.nextInt(); } } int high=-100000000; for(int m=0; m<4; m++){ for(int n=1; n<5; n++){ int one=N[m][n-1]; int one1=N[m][n]; int one2=N[m][n+1]; int one3=N[m+1][n]; int one4=N[m+2][n-1]; int one5=N[m+2][n]; int one6=N[m+2][n+1]; int add=one+one1+one2+one3+one4+one5+one6; if(add>high){ high=add; } } } System.out.println(high); } }A solution in java 8 programming.import java.io.*; import java.util.*; public class Solution { static int[][] data; public static void main(String[] args) { Scanner scan = new Scanner(System.in); data = new int[6][6]; for (int x = 0; x < 6; x++) { for (int y = 0; y < 6; y++) { data[x][y] = scan.nextInt(); } } int max = Integer.MIN_VALUE; int cur; for (int x = 1; x < 5; x++) { for (int y = 1; y < 5; y++) { cur = getHour(x, y); if (cur > max) max = cur; } } System.out.println(max); } static int getHour(int x, int y) { int sum = 0; sum += data[x-1][y-1]; //top sum += data[x-1][y]; sum += data[x-1][y+1]; sum += data[x][y]; //middle sum += data[x+1][y-1]; //bottom sum += data[x+1][y]; sum += data[x+1][y+1]; return sum; } } coding problems solutions Hackerrank Problems Solutions Java solutions HackerRankjava