Find the sum of Z shape in the given matrix

Iqram Ali
Nov 1, 2020

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…

--

--

Iqram Ali

Developer influencer, 15+ years in software development currently in engineering leadership role.