S2Q2 · Max Column Sum and Max Column Sum Index¶
⚡ Quick Reference
Type: Full I/O problem - write complete code with input() and print()
Core idea: read the matrix row by row, compute the sum of each column, find the index of the maximum column sum.
m, n = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(m)]
col_sums = [sum(matrix[r][c] for r in range(m)) for c in range(n)]
max_sum = max(col_sums)
max_idx = col_sums.index(max_sum)
print(max_idx)
print(max_sum)
Key rules:
- 0-based column index
- If multiple columns share the maximum sum, return the smallest index - list.index() already does this (returns the first occurrence)
- Print index on line 1, max sum on line 2
Problem Statement¶
Problem (I/O type)
Given an m × n matrix, find the 0-based index of the column with the largest sum. If multiple columns share the maximum sum, return the smallest index among them.
Input: First line is m n; next m lines each have n space-separated integers.
Output: Line 1 - column index. Line 2 - maximum column sum.
Example:
3 4
1 2 3 4
5 6 7 8
9 10 11 12
3
24
Understanding the problem¶
Visualising the matrix and its column sums:
Col 0 Col 1 Col 2 Col 3
Row 0: 1 2 3 4
Row 1: 5 6 7 8
Row 2: 9 10 11 12
─ ─ ─ ─
Sum: 15 18 21 24 ← column sums
The maximum sum is 24, at column index 3.
Key insight
Column c contains matrix[0][c], matrix[1][c], ..., matrix[m-1][c] - i.e. fix the column index and vary the row. This is the opposite of how you naturally read a matrix row by row, so it trips people up. The formula is: sum(matrix[r][c] for r in range(m)).
Reading the matrix¶
m, n = map(int, input().split()) # read dimensions
matrix = [
list(map(int, input().split())) # read each row as a list of ints
for _ in range(m)
]
After this, matrix[r][c] gives the element at row r, column c.
Computing column sums¶
There are two natural ways to think about this:
Approach A - iterate columns, sum down each one:
col_sums = []
for c in range(n):
total = 0
for r in range(m):
total += matrix[r][c]
col_sums.append(total)
Approach B - use zip to transpose:
zip(*matrix) transposes the matrix - it groups all first elements, all second elements, etc. - turning columns into iterables you can sum() directly.
Finding the maximum and its index¶
list.index(value) returns the first position of value - so if multiple columns have the same maximum sum, the smallest index is returned automatically. No extra logic needed.
Tracing the example¶
Matrix after reading:
Column sums:
col_sums = [15, 18, 21, 24]
max_sum = 24, max_idx = 3
Output:
Solution approaches¶
m, n = map(int, input().split())
matrix = []
for _ in range(m):
row = list(map(int, input().split()))
matrix.append(row)
col_sums = []
for c in range(n):
total = 0
for r in range(m):
total += matrix[r][c]
col_sums.append(total)
max_sum = max(col_sums)
max_idx = col_sums.index(max_sum)
print(max_idx)
print(max_sum)
Everything explicit - each step is visible and easy to follow.
m, n = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(m)]
col_sums = [sum(matrix[r][c] for r in range(m)) for c in range(n)]
max_sum = max(col_sums)
max_idx = col_sums.index(max_sum)
print(max_idx)
print(max_sum)
Compact and clean. The inner generator sum(matrix[r][c] for r in range(m)) sums one column at a time.
m, n = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(m)]
col_sums = [sum(col) for col in zip(*matrix)]
max_sum = max(col_sums)
max_idx = col_sums.index(max_sum)
print(max_idx)
print(max_sum)
zip(*matrix) is the Pythonic way to transpose a matrix. Each element yielded by zip is a tuple of one column's values, ready to sum(). Most elegant, but requires knowing the * unpacking trick.
m, n = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(m)]
col_sums = [sum(matrix[r][c] for r in range(m)) for c in range(n)]
max_idx, max_sum = max(enumerate(col_sums), key=lambda x: x[1])
print(max_idx)
print(max_sum)
max(enumerate(col_sums), key=lambda x: x[1]) finds the (index, value) pair with the largest value in one step. Returns the first maximum automatically because max scans left to right and stops at the first tie.
Tie-breaking - why list.index() works¶
The problem says: if more than one column shares the maximum sum, return the one with the smallest index.
col_sums.index(max_sum) scans from left to right and returns the first position where max_sum is found - which is always the smallest index. No sorting, no extra code needed.
Key takeaways¶
Accessing column c: matrix[r][c]
Fix c, vary r over range(m). This is the opposite of reading rows - it's easy to get backwards under exam pressure.
zip(*matrix) transposes
zip(*matrix) turns rows into columns. Each yielded tuple is one column of the matrix - you can sum() it directly.
list.index() for first maximum
list.index(max_val) always returns the first (leftmost) occurrence - which is exactly what "smallest index among ties" requires. No extra logic needed.