Given a square matrix mat, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
Example 1:
Input: mat = [[1,2,3], [4,5,6], [7,8,9]] Output: 25 Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25 Notice that element mat[1][1] = 5 is counted only once.
Example 2:
Input: mat = [[1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1]] Output: 8
Example 3:
Input: mat = [[5]] Output: 5
Constraints:
n == mat.length == mat[i].length1 <= n <= 1001 <= mat[i][j] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to find the sum of diagonals in a square grid means we'll go through the grid and check every possible cell that could be on either of the diagonals. We'll add the values of those cells together to get our final answer.
Here's how the algorithm would work step-by-step:
def matrix_diagonal_sum_brute_force(matrix):
grid_size = len(matrix)
diagonal_sum = 0
for row_index in range(grid_size):
for column_index in range(grid_size):
# Check for main diagonal (top-left to bottom-right)
if row_index == column_index:
diagonal_sum += matrix[row_index][column_index]
# Check for anti-diagonal (top-right to bottom-left), avoid double counting
if row_index + column_index == grid_size - 1 and row_index != column_index:
diagonal_sum += matrix[row_index][column_index]
return diagonal_sumThe goal is to sum the numbers found on the main and anti-diagonal of a square grid of numbers. The clever approach is to traverse the grid once and selectively add numbers to the running total. A simple adjustment avoids double-counting the center element if the grid's side length is odd.
Here's how the algorithm would work step-by-step:
def matrix_diagonal_sum(matrix):
grid_size = len(matrix)
diagonal_sum = 0
for row_index in range(grid_size):
# Add the main diagonal element.
diagonal_sum += matrix[row_index][row_index]
# Add the anti-diagonal element.
diagonal_sum += matrix[row_index][grid_size - 1 - row_index]
# Correct for double counting if grid size is odd.
if grid_size % 2 != 0:
center_index = grid_size // 2
diagonal_sum -= matrix[center_index][center_index]
return diagonal_sum| Case | How to Handle |
|---|---|
| Null or undefined input matrix | Return 0 or throw an IllegalArgumentException to avoid NullPointerException. |
| Empty matrix (matrix with zero rows) | Return 0 as there are no diagonal elements to sum. |
| Matrix with only one element (1x1 matrix) | Return the value of the single element, as it's on both diagonals. |
| Matrix with negative numbers | The solution should handle negative numbers correctly as the problem statement allows integer values. |
| Large matrix (e.g., 1000x1000) to test for performance | The solution should have O(n) time complexity where n is the size of one dimension, making it scalable. |
| Matrix with all elements being zero | The sum should be 0, handled correctly by the summation logic. |
| Matrix with extremely large integer values close to Integer.MAX_VALUE | Consider using long data type for the sum to prevent integer overflow. |
| Non-square matrix | The problem states it receives a 'square matrix' so either throw an error, or adjust to consider diagonals until we reach either row or col end if they are different |