Matrix Anti-Diagonal Sum
EasyPractice library · +10 XPmatrixtraversal
Given a square matrix of integers, compute the sum of elements that lie on its anti-diagonal. The anti-diagonal runs from the top-right corner to the bottom-left corner.
Input
The first line contains an integer N (1 <= N <= 1000), the dimension of the square matrix. The next N lines each contain N space-separated integers representing the elements of the matrix.
Output
Print a single integer, the sum of the elements on the anti-diagonal.
Constraints
1 <= N <= 1000 Matrix elements are integers between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB11 hidden tests
Example 1
Input
3 1 2 3 4 5 6 7 8 9
Output
15
The anti-diagonal elements are 3, 5, 7. Their sum is 3 + 5 + 7 = 15.
Example 2
Input
2 10 20 30 40
Output
50
The anti-diagonal elements are 20, 30. Their sum is 20 + 30 = 50.
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 1 2 3 4 5 6 7 8 9
Example 2 input
2 10 20 30 40