O17

Matrix Diagonal Difference

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: NUMERIC

Problem Statement & Objective

Given a square matrix, calculate the absolute difference between the sums of its diagonals (primary diagonal from top-left to bottom-right, and secondary diagonal from top-right to bottom-left).

Function Signature (Python)

diagonal_difference(matrix: list[list[int]]) -> int

Input & Output Contract

Output Rule:Absolute difference integer |primary_sum - secondary_sum|.

System Constraints

1 <= n <= 100
-1000 <= matris[i][j] <= 1000

Example Execution Walkthrough

matris = [[1,2,3],[4,5,6],[9,8,9]]
primary = 1 + 5 + 9 = 15; secondary = 3 + 5 + 9 = 17
|15 - 17| = 2

Example Test Scenarios (3)

Example 1
Input:[[[1,2,3],[4,5,6],[9,8,9]]]
Expected Output:2
Example 2: 1x1
Input:[[[5]]]
Expected Output:0
Example 3: (1+4)-(2+3) = 0
Input:[[[1,2],[3,4]]]
Expected Output:0
Loading Python Editor...
Input Parameters
[
  [
    [
      1,
      2,
      3
    ],
    [
      4,
      5,
      6
    ],
    [
      9,
      8,
      9
    ]
  ]
]
Expected Output
2