Matrix Diagonal Sum
Easy
matrix
array
math
Problem Description
Calculate sum of both diagonals in a square matrix. Count center element only once if matrix size is odd.
Input Format
First line contains n. Next n lines contain matrix rows.
Output Format
Sum of both diagonals
Constraints
1 ≤ n ≤ 100, matrix elements ≤ 1000
Sample Input/Output
Input:
3 1 2 3 4 5 6 7 8 9
Output:
25
Explanation
Primary diagonal: 1+5+9=15, Secondary: 3+5+7=15, Center 5 counted once: 15+15-5=25
Solution Hints
- Read carefully: Understand input/output format and constraints
- Start simple: Think of brute force solution first, then optimize
- Edge cases: Consider empty inputs, single elements, max constraints
- Data structures: Choose arrays, hash maps, sets based on problem needs
- Time complexity: Ensure your solution fits within time limits
- Test examples: Verify logic with provided sample inputs
90
Points
Points
2000ms
Time Limit
Time Limit
256MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now