Matrix Diagonal Sum

🏠 ⬅️ ➡️

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].length
  • 1 <= n <= 100
  • 1 <= mat[i][j] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 3
    integer, dimension(n, n) :: mat
    integer :: i, j
    integer :: sum_diagonal

    ! Example 1
    mat(:, :) = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], shape(mat))
    write (*, '(A, I0)') 'Example 1: ', sum_diagonal(mat)

    ! Example 2
    mat(:, :) = reshape([1, 1, 1, 1, 1, 1, 1, 1, 1], shape(mat))
    write (*, '(A, I0)') 'Example 2: ', sum_diagonal(mat)

    ! Example 3
    mat(:, :) = reshape([5], shape(mat))
    write (*, '(A, I0)') 'Example 3: ', sum_diagonal(mat)

contains

    function sum_diagonal(mat) result(sum_)
        implicit none
        integer, intent(in) :: mat(:, :)
        integer :: sum_
        integer :: i, j

        sum_ = 0
        do i = 1, size(mat, 1)
            if (i /= size(mat, 2)) then
                sum_ = sum_ + mat(i, i+1)
            end if
            sum_ = sum_ + mat(i, i)
            if (i /= 1) then
                sum_ = sum_ + mat(i-1, i)
            end if
        end do
    end function sum_diagonal

end program main
Compiled
Executed
Correct
module diagonal_sum

implicit none

contains

integer function diagonal_sum(mat) result(sum)

integer, intent(in) :: mat(:,:)

integer :: i, j

sum = 0

! Sum the elements on the primary diagonal
do i = 1, size(mat, 1)
    sum = sum + mat(i, i)
end do

! Sum the elements on the secondary diagonal
do i = 1, size(mat, 1)
    j = size(mat, 2) - i + 1
    if (i /= j) then
        sum = sum + mat(i, j)
    end if
end do

end function diagonal_sum

end module diagonal_sum

program main

use diagonal_sum

implicit none

integer, allocatable :: mat(:,:)
integer :: n, i, j

! Read the size of the matrix
read *, n

! Allocate the matrix
allocate(mat(n, n))

! Read the elements of the matrix
do i = 1, n
    do j = 1, n
        read *, mat(i, j)
    end do
end do

! Print the sum of the matrix diagonals
write (*, '(i0)') diagonal_sum(mat)

end program main
🌐 Data from online sources
class SubrectangleQueries:
    def __init__(self, rectangle: List[List[int]]):
        self.rectangle = rectangle

    def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
        for i in range(row1, row2 + 1):
            for j in range(col1, col2 + 1):
                self.rectangle[i][j] = newValue

    def getValue(self, row: int, col: int) -> int:
        return self.rectangle[row][col]

The algorithm creates a class SubrectangleQueries that takes a 2D matrix called rectangle as input. It has two methods: updateSubrectangle and getValue.

updateSubrectangle() takes five parameters: row1, col1, row2, col2, and newValue. It iterates over the subrectangle with top-left corner (row1, col1) and bottom-right corner (row2, col2) and updates all the values inside it to newValue.

getValue() is a simple getter function that takes row and col as parameters and returns the value located at rectangle[row][col].

🌐 Data from online sources
class SubrectangleQueries {
public:
    vector<vector<int>> rectangle;
    SubrectangleQueries(vector<vector<int>>& rectangle) {
        this->rectangle = rectangle;
    }

    void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        for(int i = row1; i <= row2; i++) {
            for(int j = col1; j <= col2; j++) {
                rectangle[i][j] = newValue;
            }
        }
    }

    int getValue(int row, int col) {
        return rectangle[row][col];
    }
};

The algorithm creates a class SubrectangleQueries that takes a 2D matrix called rectangle as input. It has two methods: updateSubrectangle and getValue.

updateSubrectangle() takes five parameters: row1, col1, row2, col2, and newValue. It iterates over the subrectangle with top-left corner (row1, col1) and bottom-right corner (row2, col2) and updates all the values inside it to newValue.

getValue() is a simple getter function that takes row and col as parameters and returns the value located at rectangle[row][col].