-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2DHourglass.java
More file actions
45 lines (34 loc) · 1.26 KB
/
Copy pathArray2DHourglass.java
File metadata and controls
45 lines (34 loc) · 1.26 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
import java.util.*;
import java.io.*;
public class Array2DHourglass {
static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<List<Integer>> arr = new ArrayList<>();
for (int i = 0; i < 6; i++) {
String[] arrRowTempItems = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
List<Integer> arrRowItems = new ArrayList<>();
for (int j = 0; j < 6; j++) {
int arrItem = Integer.parseInt(arrRowTempItems[j]);
arrRowItems.add(arrItem);
}
arr.add(arrRowItems);
}
bufferedReader.close();
}
static int hourglassSum(int[][] arr) {
int sum=0;
for(int i=0;i<arr[0].length-2;i++){
for(int j=0;j<arr[0].length-2;j++){
arr[i][j]=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];
}
}
sum=Integer.MIN_VALUE;
for(int i=0;i<arr[0].length-2;i++){
for(int j=0;j<arr[0].length-2;j++){
if(arr[i][j]>=sum)sum=arr[i][j];
}
}
return(sum);
}
}