-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalDifference.cs
More file actions
43 lines (37 loc) · 1018 Bytes
/
DiagonalDifference.cs
File metadata and controls
43 lines (37 loc) · 1018 Bytes
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
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using Shouldly;
using Xunit;
namespace compare_the_triplets
{
public class DiagonalDifference
{
static int diagonalDifference(int[][] arr)
{
int d1 = 0, d2 = 0;
for (int i=0; i < arr.Length; i++)
{
d1 += arr[i][i];
int j = arr.Length - i - 1;
d2 += arr[i][j];
}
return Math.Abs(d1 - d2);
}
[Fact]
public void Test1()
{
diagonalDifference(ToArr(@"11 2 4
4 5 6
10 8 -12")).ShouldBe(15);
}
int[][] ToArr(string s)
{
var arr = s.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(li => int.Parse(li))
.ToArray()).ToArray();
return arr;
}
}
}