HackerRank Java 2D Array problem solution YASH PAL, 31 July 2024 In this HackerRank Java 2D Array problem in java programming, 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 solution 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 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 hackerrank solutions java