-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
48 lines (41 loc) · 1.31 KB
/
Solution.java
File metadata and controls
48 lines (41 loc) · 1.31 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
private static final Scanner scan = new Scanner(System.in);
private static List<List<Integer>> table;
public static void main(String[] args) {
int n = scan.nextInt();
table = new ArrayList<>();
for (int i = 0; i < n; i++) {
table.add(new ArrayList<>());
for (int j = 0; j < n; j++) {
table.get(i).add(scan.nextInt());
}
}
scan.close();
// printTable(table);
System.out.println("Diagonal difference = " + Result.diagonalDifference(table));
}
private static void printTable(List<List<Integer>> t) {
for (List<Integer> row : t) {
for (Integer num : row) {
System.out.printf("%-4d ", num);
}
System.out.printf("%n");
}
}
}
class Result {
public static int diagonalDifference(List<List<Integer>> arr) {
int d1 = 0;
int d2 = 0;
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < arr.get(i).size(); j++) {
if (i == j) d1 += arr.get(i).get(j);
if (j == arr.size() - 1 - i) d2 += arr.get(i).get(j);
}
}
return Math.abs(d2 - d1);
}
}