Secondary Diagonal Sum

You are given a square matrix of size N x N. Your task is to calculate the sum of the elements present on the secondary diagonal of the matrix.

The secondary diagonal runs from the top-right corner to the bottom-left corner. Its elements have row and column indices whose sum is (N-1), using zero-based indexing.

Example 1:

Input:

Matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

Output:

15

Explanation:

The main diagonal elements are 3, 5, 7. Their sum is 3 + 5 + 7 = 15.

Example 2:

Input:

Matrix = [
    [-10, 20],
    [30, 40]
]

Output:

50

Explanation:

The main diagonal elements are 20, 30. Their sum is 20 + 30 = 50.

Now Your Turn!

Pick the correct output for the given input

Input:

Matrix = [
    [10, 2, 4],
    [-3, -4, 6],
    [8, -6, 1]
]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= N <= 100
  • -105 <= Matrix[i][j] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int secondaryDiagonalSum(int** arr, int N) {
// Write your logic here
}
};
Test Case

Input:

Matrix