Matrix Row and Column Sums
Streaming Input / 1D Array SimulationSolved Oct 23, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a matrix of size N × M, the task is to perform two separate calculations:ReadHide
Problem Description (Combined)
Given a matrix of size N × M, the task is to perform two separate calculations:
- Row Sum: Print the sum of elements for each row. Constraint: Solve this without declaring or storing the full
N × Mmatrix (i.e., streaming). - Column Sum: Print the sum of elements for each column.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Calculate Row Sums by streaming and Column Sums using an auxiliary 1D array.
Approaches
1. Matrix Row Sum (Streaming Approach) - O(N*M), O(1) Extra Space ⭐
Idea: To meet the "without storing the matrix" constraint, we must calculate the row sum as we receive the input for that row. This ensures minimal memory usage (O(1) extra space).
2. Matrix Column Sum (1D Array Simulation) - O(N*M), O(M) Extra Space
Idea: Column sums require accumulating data across all rows. We initialize a 1D array of size M (col_sums) to store the running totals for each column. This uses O(M) extra space and maintains O(N · M) time complexity.
Complexity Analysis
| Calculation | Time | Space | Notes |
|---|---|---|---|
| Row Sum (Streaming) | O(N*M) | O(1) | Constant extra space for the row sum variable. |
| Column Sum (1D Array) | O(N*M) | O(M) | Requires storage for M column totals. |
Solution code
import sys
def solve_matrix_sums():
# Set up fast input reading
try:
# Read N and M from the first line
line = sys.stdin.readline().split()
if not line:
return
N = int(line[0])
M = int(line[1])
except:
return
# O(M) Space: Initialize list to store column sums
col_sums = [0] * M
# Store the row sums for separate printing (optional, depends on platform)
row_sums_output = []
# --- Combined Input Pass (O(N*M)) ---
for i in range(N):
try:
# Read the current row's M elements
row_elements = list(map(int, sys.stdin.readline().split()))
except:
break
row_sum = 0
for j in range(M):
element = row_elements[j]
# 1. Row Sum Calculation (Streaming)
row_sum += element
# 2. Column Sum Calculation (Storage)
col_sums[j] += element
# Store Row Sum (for "Matrix Row Sum" output)
row_sums_output.append(row_sum)
# --- Print Row Sums (Output for Problem 1: Matrix Row Sum) ---
for r_sum in row_sums_output:
print(r_sum)
# --- Print Column Sums (Output for Problem 2: Matrix Column Sum) ---
# Note: Added separator for combined testing; remove for individual problem submission.
print("--- Column Sums ---")
for c_sum in col_sums:
print(c_sum)
# If running as a script, solve the problem
if __name__ == "__main__":
solve_matrix_sums()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language