Dsa
Matrix
Easy
Matrix Diagonal Sum
Given a square matrix of integers, calculate the sum of both its main and secondary diagonals. The main diagonal is defined as the diagonal from the top-left to the bottom-right, and the secondary diagonal is from the top-right to the bottom-left.
Input format:
- The first line contains a single integer N, the size of the N x N matrix.
- The next N lines each contain N integers representing the matrix elements.
Output format:
- A single line with the sum of the main diagonal and the sum of the secondary diagonal, separated by a space.
Example:
Input:
3
1 2 3
4 5 6
7 8 9
Output:
15 15
Key concepts
matrixdiagonalsummation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now