Skip to content

Instantly share code, notes, and snippets.

@husnain-iqbal
Created February 15, 2016 15:25
Show Gist options
  • Save husnain-iqbal/627c48c77acfbe3bc779 to your computer and use it in GitHub Desktop.
Save husnain-iqbal/627c48c77acfbe3bc779 to your computer and use it in GitHub Desktop.
Given a square matrix of size N×NN×N, calculate the absolute difference between the sums of its diagonals. Input Format The first line contains a single integer, NN. The next NN lines denote the matrix's rows, with each line containing NN space-separated integers describing the columns. Output Format Print the absolute difference between the two…
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.lang.*;;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[][] = new int[n][n];
int primaryDiagSum = 0;
int secondaryDiagSum = 0;
for(int a_i=0; a_i < n; a_i++){
for(int a_j=0; a_j < n; a_j++){
a[a_i][a_j] = in.nextInt();
if(a_i == a_j){
primaryDiagSum += a[a_i][a_j];
}
if(a_i+1+a_j+1 == n+1){
secondaryDiagSum += a[a_i][a_j];
}
}
}
System.out.println(Math.abs(primaryDiagSum - secondaryDiagSum));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment