Matrix Column Sum
EasyPractice library · +10 XPmatrix
Given an N x M matrix (a 2D grid of numbers), compute the sum of elements for each column independently. Print these sums, separated by spaces, in the order of the columns (from left to right).
Input
The first line contains two integers N and M, the number of rows and columns. The next N lines each contain M integers, representing the matrix elements.
Output
Print M integers, separated by spaces, where the i-th integer is the sum of the elements in the i-th column.
Constraints
1 <= N, M <= 1000. Matrix elements are integers between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Output
15 18 21 24
Sums are (1+5+9), (2+6+10), (3+7+11), (4+8+12) = 15 18 21 24.
Example 2
Input
2 2 -1 1 1 -1
Output
0 0
Sums are (-1+1), (1+-1) = 0 0.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Example 2 input
2 2 -1 1 1 -1