Compare matrix diagonals
For a square matrix return the absolute difference between the sums of its two main diagonals. The empty matrix returns zero.
n = 3
for row in range(n):
print(row, row, n-1-row)Reason about the state
At row i the two columns are i and n-1-i. Traverse the rows once using constant extra space. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
For a square matrix return the absolute difference between the sums of its two main diagonals. The empty matrix returns zero.
The explanation opens here once every check passes.