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.
Solution :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
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];
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();
}
}
System.out.println(diffrence(a,n));
}
static int diffrence(int a[][], int n){
int sumi=0;
int sumj=0;
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
if(i==j){
sumi+=a[i][j];
}
if(j==(n-i-1))
sumj+=a[i][j];
}
}
return Math.abs(sumi-sumj);
}
}
Solution :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
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];
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();
}
}
System.out.println(diffrence(a,n));
}
static int diffrence(int a[][], int n){
int sumi=0;
int sumj=0;
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++){
if(i==j){
sumi+=a[i][j];
}
if(j==(n-i-1))
sumj+=a[i][j];
}
}
return Math.abs(sumi-sumj);
}
}