Find Sum of Matrix Diagonals in Java
Problem
In a square matrix, the primary diagonal runs from the top-left to the bottom-right corner (row equals column), and the secondary diagonal runs from the top-right to the bottom-left (row plus column equals n - 1).
Given a square matrix, find the sum of its primary diagonal and the sum of its secondary diagonal.
Java Program
public class SumOfMatrixDiagonals {
public static void main(String[] args) {
int[][] matrix = {
{6, 1, 4},
{2, 7, 3},
{5, 8, 9}
};
int n = matrix.length;
int primary = 0;
int secondary = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
primary += matrix[i][j]; // top-left to bottom-right
}
if (i + j == n - 1) {
secondary += matrix[i][j]; // top-right to bottom-left
}
}
}
System.out.println("Primary diagonal sum: " + primary);
System.out.println("Secondary diagonal sum: " + secondary);
}
}Output
Core Logic
Scanning every cell and testing its row and column indexes picks out exactly the cells that lie on each diagonal.
- 1
nis the side length of the square matrix. - 2Nested loops visit every cell
matrix[i][j]. - 3
i == jmarks a primary-diagonal cell, which is added toprimary. - 4
i + j == n - 1marks a secondary-diagonal cell, which is added tosecondary.
6, 7, 9 (sum 22) and the secondary cells are 4, 7, 5 (sum 16).Key Point: The two checks are separate if statements, not else if — when n is odd, the centre cell (7 here) sits on both diagonals and must count toward both sums.
Why: All n² cells are checked even though only about 2n of them lie on a diagonal; the sums need just two variables.
Key Concepts
Approach 2: Single Loop
public class SumOfMatrixDiagonalsSingleLoop {
public static void main(String[] args) {
int[][] matrix = {
{6, 1, 4},
{2, 7, 3},
{5, 8, 9}
};
int n = matrix.length;
int primary = 0;
int secondary = 0;
for (int i = 0; i < n; i++) {
primary += matrix[i][i]; // column equals row
secondary += matrix[i][n - 1 - i]; // column mirrors row
}
System.out.println("Primary diagonal sum: " + primary);
System.out.println("Secondary diagonal sum: " + secondary);
}
}
Output
Core Logic
The diagonal cells can be addressed directly from the row number, so there's no need to visit and test every cell.
- 1One loop runs
ifrom0ton - 1. - 2
matrix[i][i]is the primary-diagonal cell in rowi. - 3
matrix[i][n - 1 - i]is the secondary-diagonal cell in the same row. - 4Both are added to their running sums in the same iteration.
i = 2 in the 3 × 3 sample, the loop reads matrix[2][2] = 9 for the primary sum and matrix[2][0] = 5 for the secondary sum.Key Point: Computing the column from the row turns an n² scan into n steps — the kind of improvement interviewers look for after the brute-force version.
Why: Each of the n rows contributes exactly one cell to each diagonal, read by direct indexing.
Key Concepts
Interview Questions
Is the centre element counted twice for odd-sized matrices?
It lies on both diagonals, so it's included in both separate sums. If one combined total is needed, subtract it once.
Does this work for rectangular matrices?
Diagonals are defined for square matrices. For a rectangle, you'd only walk as far as the shorter side.