Matrix Border Sum
Medium
matrix
array
iteration
Problem Description
Given a rectangular matrix, calculate the sum of all elements on the border (first row, last row, first column, last column). Count corner elements only once.
Input Format
First line contains m and n (matrix dimensions). Next m lines contain n space-separated integers each.
Output Format
A single integer representing the sum of border elements.
Constraints
• 1 ≤ m, n ≤ 100
• -1000 ≤ matrix elements ≤ 1000
• For 1x1 matrix, the single element is the border
• Corner elements counted only once
Sample Input/Output
Input:
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Output:
54
Explanation
Border elements are: Top row: 1+2+3+4=10, Bottom row: 9+10+11+12=42, Left column (excluding corners): 5, Right column (excluding corners): 8. Total: 10+42+5+8-4=54. We subtract 4 because corners were counted twice.
Solution Hints
70
Points
Points
2000ms
Time Limit
Time Limit
128MB
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