-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagonalDifference_15.java
50 lines (41 loc) · 1.61 KB
/
DiagonalDifference_15.java
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
package MountBlue_01;
import java.util.Scanner;
public class DiagonalDifference_15 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = input.nextInt();
}
}
int ans = diagonalDifference(arr, n);
System.out.println(ans);
}
static int diagonalDifference(int[][] arr, int n){
// Initialize variable to store the sums of diagonals
int right = 0, left = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//finding the sum of left-to-right diagonal
if(i == j){
right += arr[i][j];
}
//finding the sum of right-to-left diagonal
if(i == n - 1 - j){
left += arr[i][j];
}
}
}
//absolute difference of the sums across the diagonals
int ans = Math.abs(right - left);
return ans;
}
}
/*
1. Calculate the sums across the two diagonals of a square matrix
2. Along the first diagonal of the matrix, row index = column index that is lies on the first diagonal if i = j
3. Along the other diagonal, row index = n – 1 – column index that is mat[i][j] lies on the second diagonal if i = n-1-j
4. By using two loops we traverse the entire matrix and calculate the sum across the diagonals of the matrix.
*/