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
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
temp.f95:23:26: 23 | function sum_diagonal(mat) result(sum_) | 1 Error: Procedure ‘sum_diagonal’ at (1) has an explicit interface from a previous declaration temp.f95:24:21: 24 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:25:40: 25 | integer, intent(in) :: mat(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:26:23: 26 | integer :: sum_ | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:23: 27 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:29:16: 29 | sum_ = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:30: 30 | do i = 1, size(mat, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:31:39: 31 | if (i /= size(mat, 2)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:41: 32 | sum_ = sum_ + mat(i, i+1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:15: 33 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:34:35: 34 | sum_ = sum_ + mat(i, i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:28: 35 | if (i /= 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:36:41: 36 | sum_ = sum_ + mat(i-1, i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:37:15: 37 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:11: 38 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:39:7: 39 | end function sum_diagonal | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:18:24: 18 | mat(:, :) = reshape([5], shape(mat)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
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
temp.f95:7:29: 7 | integer function diagonal_sum(mat) result(sum) | 1 Error: MODULE attribute of ‘diagonal_sum’ conflicts with PROCEDURE attribute at (1) temp.f95:9:31: 9 | integer, intent(in) :: mat(:,:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:15: 11 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:7: 13 | sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:22: 16 | do i = 1, size(mat, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:25: 17 | sum = sum + mat(i, i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:3: 18 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:22: 21 | do i = 1, size(mat, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:22:28: 22 | j = size(mat, 2) - i + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:20: 23 | if (i /= j) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:29: 24 | sum = sum + mat(i, j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:25:7: 25 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:3: 26 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:3: 28 | end function diagonal_sum | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:5: 34 | use diagonal_sum | 1 Fatal Error: Cannot open module file ‘diagonal_sum.mod’ for reading at (1): No such file or directory compilation terminated.
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].
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].