-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalDifference.java
More file actions
63 lines (43 loc) · 1.26 KB
/
Copy pathDiagonalDifference.java
File metadata and controls
63 lines (43 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.
Input Format
The first line contains a single integer, . The next lines denote the matrix's rows, with each line containing space-separated integers describing the columns.
Output Format
Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [][] arr = new int[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
arr[i][j] = scan.nextInt();
}
}
int num1 = 0;
int num2 = 0;
int num3 = 0;
int j = 0;
int c = n - 1;
for(int i = 0; i < n; i++) {
num1 += arr[i][j++];
num2 += arr[i][c--];
}
num3 = num1 - num2;
if(num3 < 0) {
num3 *= -1;
}
scan.close();
System.out.println(num3);
}
}