Sign in

Find the sum of Z shape in the given matrix

Approach1:

Simple algorithm

  • n := row count of matrix
  • if n <= 2, then
  • return sum of all elements in matrix
  • first_row := sum of first row
  • last_row := sum of last row
  • diagonal = sum of matrix[i, n-1-i] for all i from 1 to n-2
  • return first_row + last_row + diagonal
class SumZshape:
def solve(self, matrix):
n = len(matrix)
if n <= 2…