-
Notifications
You must be signed in to change notification settings - Fork 20
/
DiagonalMatrix.java
36 lines (32 loc) · 1 KB
/
DiagonalMatrix.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
public class DiagonalMatrix {
public static void main(String[] args) {
int matrix[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
// System.out.println(matrix[0].length);
Diag(matrix);
}
public static void Diag(int [][]a){
int sum =0;
// for(int i=0;i<a.length;i++){
// for(int j=0;j<a[0].length;j++){
// //primary
// if(i==j){
// sum += a[i][j];
// }
// if(i+j==a.length-1){
// sum+=a[i][j];
// // System.out.println(a[i][j]);
// }
// }
// }System.out.println(sum);
//-----------optimised code----------------
for(int i=0;i<a.length;i++){
//primary
sum+=a[i][i];
//secondary
if(i!= a.length-1-i){
sum+= a[i][a.length-1-i];
}
}
System.out.println(sum);
}
}