Skip to content

Instantly share code, notes, and snippets.

@eriksjaastad
Last active September 9, 2015 11:54
Show Gist options
  • Save eriksjaastad/5406ba7c242ef0f12bd4 to your computer and use it in GitHub Desktop.
Save eriksjaastad/5406ba7c242ef0f12bd4 to your computer and use it in GitHub Desktop.
Hacker Rank

####Diagnol Difference

Problem Statement

You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.

Input Format

The first line contains a single integer N. The next N lines contain N integers (each) describing the matrix.

Constraints 1≤N≤100 −100≤A[i]≤100 Output Format

Output a single integer equal to the absolute difference in the sums across the diagonals.

Sample Input

3
11 2 4
4 5 6
10 8 -12

Sample Output

15

Explanation

The first diagonal of the matrix is:

11
    5
        -12

Sum across the first diagonal = 11+5-12= 4

The second diagonal of the matrix is:

        4
    5
10

Sum across the second diagonal = 4+5+10 = 19

Difference: |4-19| =15

function processData(input) {
    var lines = input.trim().split(/\s+/);
    var n = 1 * lines.shift();
    var sum = 0;
    var a = 0;
    var b = n - 1;
    while(a < lines.length) {
        sum += lines[a] - lines[b];
        a += n + 1;
        b += n - 1;
    }
    console.log(Math.abs(sum));
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment