Wednesday, August 30, 2017

Square Matrix - Corner Elements Sum

A square matrix of size N×N is passed as the input. The program must calculate and print the sum of the elements in the corners.

Input Format:
The first line will contain the value of N. The next N lines will contain the N values separated by one or more spaces.

Output Format:
The first line will contain the integer value denoting the sum of the elements in the corners.

Boundary Conditions: 2 <= N <= 20

Example Input/Output 1:
Input:
3
10 90 1 4 22 5 32 8 66

Output:
109

Explanation:
The sum = 10+1+66+32 = 109

Solution:
n=int(input())
a=[[int(i) for i in input().split()]for _ in range(n)]
print(a[0][0]+a[0][n-1]+a[n-1][0]+a[n-1][n-1])
Share: