aboutsummaryrefslogtreecommitdiff
path: root/matrix_diagonal_sum.cpp
blob: 90e7fd5f6d4b6cd3c664c00abd35e7f729cccada (plain)
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
// 1572. Matrix Diagonal Sum
#include "leetcode.h"

/*
 * 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
 */

class Solution {
public:
  int diagonalSum(vvd & mat) {
    int n = mat.size(), ans = 0;
    for (int i = 0; i < n; i++) {
      if (i != n - i - 1)
        ans += mat[i][i] + mat[i][n - i - 1];
      else
        ans += mat[i][i];
    }
    return ans;
  }
};

int main() {
  Solution obj;
  vvd m1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  vvd m2 = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};
  vvd m3 = {{5}};
  printf("%d\n", obj.diagonalSum(m1)); // expect: 25
  printf("%d\n", obj.diagonalSum(m2)); // expect: 8
  printf("%d\n", obj.diagonalSum(m3)); // expect: 5
}