Matrix Row Sums
EasyPractice library · +10 XPmatrixloopsbasic
Given a matrix (2D grid) of integers, compute the sum of elements for each row. Print these sums, separated by spaces, on a single line.
Input
The first line contains two integers N and M, representing the number of rows and columns. The next N lines each contain M integers, representing the elements of the matrix.
Output
A single line containing N space-separated integers, where the i-th integer is the sum of elements in the i-th row of the matrix.
Constraints
1 <= N, M <= 1000; -1000 <= matrix[i][j] <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB12 hidden tests
Example 1
Input
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Output
10 26 42
Sums of rows are 10, 26, 42.
Example 2
Input
2 2 -1 1 0 0
Output
0 0
Sums of rows are 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 0 0