Forked from kaibrabo/Diagonal Difference - Hackerrank
Last active
October 16, 2017 05:19
-
-
Save flagoworld/43d7bf4aee80a21e69244b9733bca203 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Parse input | |
n = gets.strip.to_i | |
a = Array.new(n) | |
(0...n).each { |a_i| | |
a_t = gets.strip | |
a[a_i] = a_t.split(' ').map(&:to_i) | |
arr = a[a_i] | |
} | |
# Initialize variables | |
a_diagonal = 0 | |
b_diagonal = 0 | |
# Sum diagonals | |
(0...arr.length).each { |i| | |
# Top left to bottom right | |
a_diagonal += arr[i][i] | |
# Top right to bottom left | |
b_diagonal += arr[i][arr.length - 1 - i] | |
} | |
# Abs of difference of diagonal sums | |
(a_diagonal - b_diagonal).abs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment