O17
Matrix Diagonal Difference
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]]) -> intInput & Output Contract
Output Rule:Absolute difference integer |primary_sum - secondary_sum|.
System Constraints
1 <= n <= 100
-1000 <= matris[i][j] <= 1000Example Execution Walkthrough
matris = [[1,2,3],[4,5,6],[9,8,9]]
primary = 1 + 5 + 9 = 15; secondary = 3 + 5 + 9 = 17
|15 - 17| = 2Example Test Scenarios (3)
Example 1
Input:
[[[1,2,3],[4,5,6],[9,8,9]]]Expected Output:
2Example 2: 1x1
Input:
[[[5]]]Expected Output:
0Example 3: (1+4)-(2+3) = 0
Input:
[[[1,2],[3,4]]]Expected Output:
0