Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

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 solutions Hackerrank Problems Solutions Java Programming Tutorials

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes