-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (26 loc) · 857 Bytes
/
Solution.java
File metadata and controls
30 lines (26 loc) · 857 Bytes
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
package Practice.Algorithms.Warmup.DiagonalDifference;
import java.util.Scanner;
public class Solution {
static int diagonalDifference(int[][] a) {
int diagonalSumLTR = 0;
int diagonalSumRTL = 0;
for (int i = 0; i < a.length; i++) {
diagonalSumLTR += a[i][i];
diagonalSumRTL += a[i][(a.length - 1) - i];
}
return Math.abs(diagonalSumLTR - diagonalSumRTL);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] a = new int[n][n];
for (int a_i = 0; a_i < n; a_i++) {
for (int a_j = 0; a_j < n; a_j++) {
a[a_i][a_j] = in.nextInt();
}
}
int result = diagonalDifference(a);
System.out.println(result);
in.close();
}
}