Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Matrix Diagonal Sum (easy)
On this page

Problem Statement

Given a square matrix (2D array), calculate the sum of its two diagonals.

The two diagonals in consideration are the primary diagonal that spans from the top-left to the bottom-right and the secondary diagonal that spans from top-right to bottom-left. If a number is part of both diagonals (which occurs only for odd-sized matrices), it should be counted only once in the sum.

Examples

  1. Example 1:
    • Input:
      [[1,2,3],
       [4,5,6],
       [7,8,9]]
      
    • Expected Output: 25

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page