-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiagonalSum.java
50 lines (42 loc) · 1.21 KB
/
DiagonalSum.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
public class DiagonalSum {
public static int diagonalSum(int[][] matrix) {
int n = matrix.length; //row
int m = matrix[0].length; //column
int sum = 0;
//Big O(n^2) approach
//Primary Diagonal
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == j) {
sum += matrix[i][j];
}
}
}
//Secondary Diagonal
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i + j == n-1) {
sum += matrix[i][j];
}
}
}*/
//Big O(n) approach
for (int i = 0; i < n; i++) {
//PD
sum += matrix[i][i];
//SD
if (i != n - 1 - i) {
sum += matrix[i][m - 1 - i];
}
}
return sum;
}
public static void main(String[] para_coder) {
/*int[][] matrix = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};*/
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(diagonalSum(matrix));
}
}