Hackerrank Java 2D Array Solution
- Published
- Updated
- Reading time
- 5 min read
- Length
- 655 words
You are given a 2D array. An hourglass in an array is a portion shaped like this:
a b c
d
e f gFor example, if we create an hourglass using the number 1 within an array full of zeros, it may look like this:
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 0Actually, 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 0The 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 0Sample Output
19Explanation
The hourglass which has the largest sum is:
2 4 4
2
1 2 4Solution in java8#
Approach 1.
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];
int maxSum = Integer.MIN_VALUE;;
int sum = 0;
for(int i=0; i < 6; i++)
{
for(int j=0; j < 6; j++)
{
arr[i][j] = in.nextInt();
if(i > 1 && j > 1)
{
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);
}
}Approach 2.
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) {
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);
}
}Approach 3.
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 i=0; i < 6; i++){
for(int j=0; j < 6; j++){
arr[i][j] = in.nextInt();
}
}
int score = -100;
for(int j=0; j < 4; j++){
for(int i=0; i < 4; i++){
// if(arr[i][j]==0||arr[i+1][j]==0||
// arr[i+2][j]==0||arr[i+1][j+1]==0||
// arr[i][j+2]==0||arr[i+1][j+2]==0||
// arr[i+2][j+2]==0)continue;
int sum = arr[i][j]+arr[i][j+2]+arr[i+2][j]+arr[i+2][j+2]+arr[i+1][j+1];
sum += arr[i][j+1] + arr[i+2][j+1];
if(sum>score)score=sum;
}
}
// System.out.println(arr[x][y]+" "+arr[x+1][y]+" "+arr[x+2][y]+"\n "+arr[x+1][y+1]+"\n"
// +arr[x][y+2]+" "+arr[x+1][y+2]+" "+arr[x+2][y+2]);
System.out.print(score);
}
}