Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . 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 Java 2D Array -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.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java 2D Array – Hacker Rank Solution
Java 2D Array – Hacker Rank Solution
Problem :
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
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.
Input Format
There will be exactly lines, each containing integers seperated by spaces. Each integer will be between and inclusive.
Output Format
Print the answer to this problem on a single line.
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
The hourglass which has the largest sum is:
2 4 4 2 1 2 4
Java 2D Array – Hacker Rank 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 in = new Scanner(System.in); int arr[][] = new int[6][6]; for(int arr_i=0; arr_i < 6; arr_i++){ for(int arr_j=0; arr_j < 6; arr_j++){ arr[arr_i][arr_j] = in.nextInt(); } } Sum(arr); } private static void Sum(int arr[][]){ //ROw int sum=-1000; for(int i =0 ; i<4;i++){ for(int x =0 ; x<4; x++){ int top = arr[i][x]+arr[i][x+1]+arr[i][x+2]; int middle = arr[i+1][x+1]; int bottom = arr[i+2][x]+arr[i+2][x+1]+arr[i+2][x+2]; if(top+middle+bottom>sum){sum=top+middle+bottom;} } } System.out.println(sum); } }
Good day! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?