Java Programs›Arrays›Find Sum of Matrix Diagonals

Find Sum of Matrix Diagonals in Java

beginner·  Arrays  ·  Matrix

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.

Input
[[6, 1, 4], [2, 7, 3], [5, 8, 9]]
Output
Primary: 22, Secondary: 16

Java Program

Java
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

Primary diagonal sum: 22 Secondary diagonal sum: 16

Core Logic

Scanning every cell and testing its row and column indexes picks out exactly the cells that lie on each diagonal.

How It Works
  1. 1n is the side length of the square matrix.
  2. 2Nested loops visit every cell matrix[i][j].
  3. 3i == j marks a primary-diagonal cell, which is added to primary.
  4. 4i + j == n - 1 marks a secondary-diagonal cell, which is added to secondary.
In the 3 × 3 sample, the primary cells are 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.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

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

2D arraynested for loopindex conditions

Approach 2: Single Loop

Java
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

Primary diagonal sum: 22 Secondary diagonal sum: 16

Core Logic

The diagonal cells can be addressed directly from the row number, so there's no need to visit and test every cell.

How It Works
  1. 1One loop runs i from 0 to n - 1.
  2. 2matrix[i][i] is the primary-diagonal cell in row i.
  3. 3matrix[i][n - 1 - i] is the secondary-diagonal cell in the same row.
  4. 4Both are added to their running sums in the same iteration.
When 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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Each of the n rows contributes exactly one cell to each diagonal, read by direct indexing.

Key Concepts

direct indexingsingle for loop

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.

Learn the Concept

Related Programs