Sum of rows and columns

Given an matrix, our task is to find the sum of all the elements in each row, as well as the sum of all the elements in each column of that matrix.
The first line of the input contains a single integer n. The next n lines contain n integers which represent the matrix.
The output should contain 2 rows:
  1. The first row should contain the sum of elements in each row, separated by space.
  1. The second row should contain the sum of elements in each column, separated by space.
Input
Output
3 0 1 2 1 8 3 -1 3 10
3 12 12 0 12 15
3 0 -1 0 0 0 0 1 0 1
-1 0 2 1 -1 1

Explanation for the 1st example:

  • First line (sum of rows):
      1. 0 + 1 + 2 = 3
      1. 1 + 8 + 3 = 12
      1. -1 + 3 + 10 = 12
  • Second line (sum of columns):
      1. 0 + 1 + (-1) = 0
      1. 1 + 8 + 3 = 12
      1. 2 + 3 + 10 = 15

Explanation for the 2nd example:

  • First line (sum of rows):
      1. 0 + (-1) + 0 = -1
      1. 0 + 0 + 0 = 0
      1. 1 + 0 + 1 = 2
  • Second line (sum of columns)
      1. 0 + 0 + 1 = 1
      1. -1 + 0 + 0 = -1
      1. 0 + 0 + 1 = 1
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue