Dsa
Matrix
Medium
Matrix Diagonal Sums
Given a square matrix, calculate the sums of its primary and secondary diagonals. The primary diagonal runs from the top left to the bottom right, while the secondary diagonal runs from the top right to the bottom left.
Input format:
- The first line contains an integer 'n', the size of the n x n matrix.
- The next 'n' lines each contain 'n' space-separated integers.
Output format:
- Print two integers: the sum of the primary diagonal and the sum of the secondary diagonal.
Example:
Input:
3
1 2 3
4 5 6
7 8 9
Output:
15 15
Key concepts
matrixdiagonalsumming
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