Sum of both diagonals
Given a square matrix, print the sum of its main diagonal and its other diagonal. If N is odd, count the centre element only once.
Reported in: Infosys (2022–24), Wipro Elite NTH (memory-based)
Input
First line: N. Next N lines: N integers each.
Output
The diagonal sum.
Constraints
1 ≤ N ≤ 1000.
Example 1
Input:
3
1 2 3
4 5 6
7 8 9
Output:
25Example 2
Input:
2
1 2
3 4
Output:
10Hint: Add m[i][i] and m[i][N−1−i]; subtract the centre once when N is odd.