Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

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

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes