July 6, 2016 Andrey

Diagonal Difference from HackerRank in Swift

Problem

Given a square matrix of size , calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, . The next lines denote the matrix’s rows, with each line containing space-separated integers describing the columns.

Output Format

Print the absolute difference between the two sums of the matrix’s diagonals as a single integer.

Sample Input

3
11 2 4
4 5 6
10 8 -12
Sample Output

15
Explanation

The primary diagonal is:
11
5
-12

Sum across the primary diagonal: 11 + 5 – 12 = 4

The secondary diagonal is:
4
5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 – 19| = 15

Solution

// read the integer n
var n = Int(readLine()!)!
// declare 2d array
var arr : [[Int]] = []

// read array row-by-row
for index in 0..<n {
    arr.append(readLine()!.characters.split(" ").map{Int(String($0))!})
}
var preimaryDiagonalSum = 0
var secondaryDiagonalSum = 0
for index in 0..<n {
    preimaryDiagonalSum = preimaryDiagonalSum + arr[index][index]
    secondaryDiagonalSum = secondaryDiagonalSum + arr[index][n-index-1]
}

print(abs(preimaryDiagonalSum - secondaryDiagonalSum))