Introduction

Row & Column Sum means calculating the total sum of:

  • each row
  • each column

inside a matrix.

The task is to:

  • traverse matrix
  • calculate row sums
  • calculate column sums

Example:

Input Matrix:1 2 3
4 5 6
7 8 9
Row Sums:
6 15 24
Column Sums:
12 15 18

Explanation:

Row 1:
1 + 2 + 3 = 6
Column 1:
1 + 4 + 7 = 12

This problem is one of the most important applications of:

Nested Loops 

Constraints

1 <= Rows, Columns <= 10^3 

Approach 1 : Row Sum Calculation

Explanations:

Explanation:

The idea is:

  • traverse row by row
  • calculate sum for each row

Steps:

  1. Start from first row.
  2. Traverse all columns.
  3. Add row elements.
  4. Print row sum.

Dry Run

Matrix:1 2 3
4 5 6
7 8 9
Row 1:
1 + 2 + 3 = 6
Row 2:
4 + 5 + 6 = 15
Row 3:
7 + 8 + 9 = 24

Practice :

Complexity Analysis :

Time Complexity:- O(rows × cols)Explanation :
Every matrix element is visited once. Space Complexity:- O(1)
Explanation :

No extra space is used.

Approach 2 : Column Sum Calculation

Explanations:

Explanation:

The idea is:

  • traverse column by column
  • calculate sum for each column

This changes traversal order.

Dry Run

Matrix:1 2 3
4 5 6
7 8 9
Column 1:
1 + 4 + 7 = 12
Column 2:
2 + 5 + 8 = 15
Column 3:
3 + 6 + 9 = 18

Practice :

Complexity Analysis :

Time Complexity:- O(rows × cols)Explanation :
Every matrix element is visited once.
Space Complexity:- O(1)
Explanation :

No extra space is used.

Why This Problem is Important

This problem builds the foundation for:

  • Matrix traversal
  • Nested loops
  • Row & column operations
  • Grid manipulation
  • 2D array handling

Real-World Applications

Matrix sum concepts are used in:

  • Spreadsheet software
  • Data analytics
  • Image processing
  • Scientific computing
  • Game development

Common Beginner Mistakes

  • Incorrect row/column indexing
  • Wrong loop order
  • Not resetting sum variable
  • Out of bounds errors
  • Confusing rows and columns

Interview Tip

Interviewers often expect:

  • proper nested loop usage
  • correct indexing
  • matrix understanding
  • efficient traversal logic

Always explain:

  • row sum logic
  • column sum logic

Related Questions

  • Matrix Traversal
  • Spiral Matrix
  • Rotate Matrix
  • Search in Matrix
  • Matrix Transpose

Final Takeaway

The Row & Column Sum problem is one of the most important beginner matrix problems.

It teaches:

  • matrix traversal
  • nested loops
  • row & column operations
  • grid manipulation

Understanding this problem builds a strong foundation for:

  • advanced matrix problems
  • graph traversal concepts
  • interview-level data structure questions.