-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
51 lines (43 loc) · 1.52 KB
/
Solution.java
File metadata and controls
51 lines (43 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// https://www.hackerrank.com/challenges/java-2d-array/problem
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static int getHourGlassSum(int x, int y, int[][] arr) {
if (x + 2 >= arr[0].length) return Integer.MIN_VALUE;
if (y + 2 >= arr.length) return Integer.MIN_VALUE;
return
arr[y][x] + arr[y][x + 1] + arr[y][x + 2] +
arr[y + 1][x + 1] +
arr[y + 2][x] + arr[y + 2][x + 1] + arr[y + 2][x + 2];
}
private static int calculateMaxHourglassSum(int[][] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
int n = getHourGlassSum(j, i, arr);
if (n > max) max = n;
}
}
return max;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[][] arr = new int[6][6];
for (int i = 0; i < 6; i++) {
String[] arrRowItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int j = 0; j < 6; j++) {
int arrItem = Integer.parseInt(arrRowItems[j]);
arr[i][j] = arrItem;
}
}
int max = calculateMaxHourglassSum(arr);
System.out.println(max);
scanner.close();
}
}