/
DiagonalDifference.cs
38 lines (30 loc) · 979 Bytes
/
DiagonalDifference.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace _02.Diagonal_Difference
{
using System;
using System.Linq;
public class DiagonalDifference
{
public static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
long[][] matrix = new long[n][];
for (int i = 0; i < n; i++)
{
matrix[i] = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
}
//Primary diagonal:
long primarySum = 0;
for (int row = 0; row < n; row++)
{
primarySum += matrix[row][row];
}
//Secondary diagonal:
long secondarySum = 0;
for (int row = 0, col = n - 1; row < n; row++, col--)
{
secondarySum += matrix[row][col];
}
Console.WriteLine(Math.Abs(primarySum - secondarySum));
}
}
}