Last active
March 29, 2017 16:57
-
-
Save ranapu/f9a34a2d0a129259eeeb8433ef363d9e to your computer and use it in GitHub Desktop.
Diagonal Difference in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "math" | |
func main() { | |
var n int | |
fmt.Scan(&n) | |
var d1, d2, e int | |
for i := 0; i < n; i++ { | |
for j := 0; j < n; j++ { | |
fmt.Scan(&e) | |
if i == j { | |
d1 += e | |
} | |
if j == n - i - 1 { | |
d2 += e | |
} | |
} | |
} | |
fmt.Print(math.Abs(float64(d1 - d2))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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