-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridSum.java
More file actions
46 lines (40 loc) · 1.34 KB
/
GridSum.java
File metadata and controls
46 lines (40 loc) · 1.34 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
package array;
import java.util.Scanner;
public class GridSum {
public static int solution1(int len, int[][] inputArray) {
int result = 0;
int widthSumValue = 0, heightSumValue = 0, crossSumValue = 0, reverseCrossValue=0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
widthSumValue += inputArray[i][j];
heightSumValue += inputArray[j][i];
}
int maxValue = Math.max(widthSumValue, heightSumValue);
if (result < maxValue) {
result = maxValue;
}
widthSumValue = 0;
heightSumValue = 0;
}
for (int i = 0; i < len; i++) {
crossSumValue += inputArray[i][i];
reverseCrossValue += inputArray[i][len -i - 1];
}
int maxValue = Math.max(crossSumValue, reverseCrossValue);
if (result < maxValue) {
result = maxValue;
}
return result;
}
public void main() {
Scanner in=new Scanner(System.in);
int len = in.nextInt();
int[][] inputArray = new int[len][len];
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
inputArray[i][j] = in.nextInt();
}
}
System.out.println(solution1(len, inputArray));
}
}