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

0% completed

Problem 2: Matrix Diagonal Sum (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible