Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Day 11: 2D Arrays in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Link for the Problem – Day 11: 2D Arrays – Hacker Rank Solution
Day 11: 2D Arrays – Hacker Rank Solution
Problem:
Objective
Today, we are building on our knowledge of arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video.
Context
Given a 2D Array, :
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
We define an hourglass in to be a subset of values with indices falling in this pattern in ‘s graphical representation:
a b c d e f g
There are hourglasses in , and an hourglass sum is the sum of an hourglass’ values.
Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Example
In the array shown above, the maximum hourglass sum is for the hourglass in the top left corner.
Input Format
There are lines of input, where each line contains space-separated integers that describe the 2D Array .
Constraints
Output Format
Print the maximum hourglass sum in .
Sample Input
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
19
Explanation
contains the following hourglasses:
1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 2 0 2 4 2 4 4 4 4 0 1 1 1 1 1 0 1 0 0 0 0 0 0 2 4 4 0 0 0 0 0 2 0 2 0 2 0 0 0 0 2 0 2 4 2 4 4 4 4 0 0 0 2 0 0 0 1 0 1 2 1 2 4 2 4 0
The hourglass with the maximum sum () is:
2 4 4 2 1 2 4
Day 11: 2D Arrays – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class Day112DArrays { 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(); } } in.close(); int sum = 0, maxSum = Integer.MIN_VALUE; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if ((i + 2) < 6 && (j + 2) < 6) { sum = 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]; } if (sum > maxSum) { maxSum = sum; } } } System.out.println(maxSum); } }