Given a matrix of n X n. The task is to calculate the absolute difference between the sums of its diagonal.
Examples:
Input : mat[][] = 11 2 4
4 5 6
10 8 -12
Output : 15
Sum of primary diagonal = 11 + 5 + (-12) = 4.
Sum of secondary diagonal = 4 + 5 + 10 = 19.
Difference = |19 - 4| = 15.
Input : mat[][] = 10 2
4 5
Output : 9
Calculate the sums across the two diagonals of a square matrix. Along the first diagonal of the matrix, row index = column index i.e mat[i][j] lies on the first diagonal if i = j. Along the other diagonal, row index = n – 1 – column index i.e mat[i][j] lies on the second diagonal if i = n-1-j. By using two loops we traverse the entire matrix and calculate the sum across the diagonals of the matrix.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
int difference( int arr[][MAX], int n)
{
int d1 = 0, d2 = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (i == j)
d1 += arr[i][j];
if (i == n - j - 1)
d2 += arr[i][j];
}
}
return abs (d1 - d2);
}
int main()
{
int n = 3;
int arr[][MAX] =
{
{11, 2, 4},
{4 , 5, 6},
{10, 8, -12}
};
cout << difference(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int difference( int arr[][], int n)
{
int d1 = 0 , d2 = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (i == j)
d1 += arr[i][j];
if (i == n - j - 1 )
d2 += arr[i][j];
}
}
return Math.abs(d1 - d2);
}
public static void main(String[] args)
{
int n = 3 ;
int arr[][] =
{
{ 11 , 2 , 4 },
{ 4 , 5 , 6 },
{ 10 , 8 , - 12 }
};
System.out.print(difference(arr, n));
}
}
|
Python3
def difference(arr, n):
d1 = 0
d2 = 0
for i in range ( 0 , n):
for j in range ( 0 , n):
if (i = = j):
d1 + = arr[i][j]
if (i = = n - j - 1 ):
d2 + = arr[i][j]
return abs (d1 - d2);
n = 3
arr = [[ 11 , 2 , 4 ],
[ 4 , 5 , 6 ],
[ 10 , 8 , - 12 ]]
print (difference(arr, n))
|
C#
using System;
public class GFG
{
public static int difference( int [,] arr,
int n)
{
int d1 = 0, d2 = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (i == j)
d1 += arr[i, j];
if (i == n - j - 1)
d2 += arr[i, j];
}
}
return Math.Abs(d1 - d2);
}
public static void Main()
{
int n = 3;
int [,] arr ={{11, 2, 4},
{4 , 5, 6},
{10, 8, -12}};
Console.Write(difference(arr, n));
}
}
|
PHP
<?php
function difference( $arr , $n )
{
$d1 = 0; $d2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if ( $i == $j )
$d1 += $arr [ $i ][ $j ];
if ( $i == $n - $j - 1)
$d2 += $arr [ $i ][ $j ];
}
}
return abs ( $d1 - $d2 );
}
{
$n = 3;
$arr = array ( array (11, 2, 4),
array (4 , 5, 6),
array (10, 8, -12));
echo difference( $arr , $n );
return 0;
}
?>
|
Javascript
<script>
function difference(arr,n)
{
let d1 = 0, d2 = 0;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
if (i == j)
d1 += arr[i][j];
if (i == n - j - 1)
d2 += arr[i][j];
}
}
return Math.abs(d1 - d2);
}
let n = 3;
let arr =
[
[11, 2, 4],
[4 , 5, 6],
[10, 8, -12]
];
document.write(difference(arr, n));
</script>
|
Time complexity: O(n*n)
Auxiliary Space: O(1) using constant space to initialize diagonal sums, since no extra space has been taken.
We can optimize the above solution to work in O(n) using the patterns present in indexes of cells.
C++
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
int difference( int arr[][MAX], int n)
{
int d1 = 0, d2 = 0;
for ( int i = 0; i < n; i++)
{
d1 += arr[i][i];
d2 += arr[i][n-i-1];
}
return abs (d1 - d2);
}
int main()
{
int n = 3;
int arr[][MAX] =
{
{11, 2, 4},
{4 , 5, 6},
{10, 8, -12}
};
cout << difference(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int difference( int arr[][], int n)
{
int d1 = 0 , d2 = 0 ;
for ( int i = 0 ; i < n; i++)
{
d1 += arr[i][i];
d2 += arr[i][n-i- 1 ];
}
return Math.abs(d1 - d2);
}
public static void main(String[] args)
{
int n = 3 ;
int arr[][] =
{
{ 11 , 2 , 4 },
{ 4 , 5 , 6 },
{ 10 , 8 , - 12 }
};
System.out.print(difference(arr, n));
}
}
|
Python3
def difference(arr, n):
d1 = 0
d2 = 0
for i in range ( 0 , n):
d1 = d1 + arr[i][i]
d2 = d2 + arr[i][n - i - 1 ]
return abs (d1 - d2)
n = 3
arr = [[ 11 , 2 , 4 ],
[ 4 , 5 , 6 ],
[ 10 , 8 , - 12 ]]
print (difference(arr, n))
|
C#
using System;
public class GFG
{
public static int difference( int [,] arr,
int n)
{
int d1 = 0, d2 = 0;
for ( int i = 0; i < n; i++)
{
d1 += arr[i, i];
d2 += arr[i, n - i - 1];
}
return Math.Abs(d1 - d2);
}
public static void Main()
{
int n = 3;
int [,] arr ={{11, 2, 4},
{4 , 5, 6},
{10, 8, -12}};
Console.Write(difference(arr, n));
}
}
|
PHP
<?php
function difference( $arr , $n )
{
$d1 = 0; $d2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$d1 += $arr [ $i ][ $i ];
$d2 += $arr [ $i ][ $n - $i -1];
}
return abs ( $d1 - $d2 );
}
{
$n = 3;
$arr = array ( array (11, 2, 4),
array (4, 5, 6),
array (10, 8, -12));
echo difference( $arr , $n );
return 0;
}
?>
|
Javascript
<script>
function difference(arr,n)
{
let d1 = 0, d2 = 0;
for (let i = 0; i < n; i++)
{
d1 += arr[i][i];
d2 += arr[i][n-i-1];
}
return Math.abs(d1 - d2);
}
let n = 3;
let arr = [[11, 2, 4],
[4 , 5, 6],
[10, 8, -12]];
document.write(difference(arr, n));
</script>
|
Time complexity : O(n)
Auxiliary Space: O(1) using constant space to initialize diagonal sums
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.