-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_diagonal_sum.cpp
84 lines (63 loc) · 1.88 KB
/
Matrix_diagonal_sum.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
1572. Matrix Diagonal Sum
Easy
1593
23
Add to List
Share
Given a square matrix mat, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.*/
//MY SOLUTION
class Solution {
public:
int getprimarySum(vector<vector<int>>& mat){
int primarySum=0;
for(int i=0;i<mat.size();i++){
primarySum = primarySum+mat[i][i];
}
cout<<primarySum<<endl;
return primarySum;
}
int getsecondarySum(vector<vector<int>>& mat){
int secondarySum=0;
for(int i=0;i<mat.size();i++){
secondarySum = secondarySum+mat[i][mat.size()-i-1];
}
cout<<secondarySum<<endl;
return secondarySum;
}
int diagonalSum(vector<vector<int>>& mat) {
if(mat.size()==1){
return mat[0][0];
}
if(mat.size()%2!=0){
return getprimarySum(mat)+getsecondarySum(mat)-mat[mat.size()/2][mat.size()/2];
}
else{
return getprimarySum(mat)+getsecondarySum(mat);
}
}
};
//OPTIMAL SOLUTION
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
// side length
int n = mat.size();
// mid point index
int mid = n / 2;
// summation of diagonal element
int summation = 0;
for( int i = 0 ; i < n ; i++ ){
// primary diagonal
summation += mat[i][i];
// secondary diagonal
summation += mat[n-1-i][i];
}
if( n % 2 == 1 ){
// remove center element (repeated) on odd side-length case
summation -= mat[mid][mid];
}
return summation;
}
};