# Find difference between sums of two diagonals

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 primary diagonal = 4 + 5 + 10 = 19.
Difference = |19 - 4| = 15.

Input : mat[][] = 10 2
4 5
Output : 7
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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++

 `// C++ program to find the difference ` `// between the sum of diagonal. ` `#include ` `#define MAX 100 ` `using` `namespace` `std; ` ` `  `int` `difference(``int` `arr[][MAX], ``int` `n) ` `{ ` `    ``// Initialize sums of diagonals ` `    ``int` `d1 = 0, d2 = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0; j < n; j++) ` `        ``{ ` `            ``// finding sum of primary diagonal ` `            ``if` `(i == j) ` `                ``d1 += arr[i][j]; ` ` `  `            ``// finding sum of secondary diagonal ` `            ``if` `(i == n - j - 1) ` `                ``d2 += arr[i][j]; ` `        ``} ` `    ``} ` ` `  `    ``// Absolute difference of the sums ` `    ``// across the diagonals ` `    ``return` `abs``(d1 - d2); ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``int` `n = 3; ` ` `  `    ``int` `arr[][MAX] = ` `    ``{ ` `        ``{11, 2, 4}, ` `        ``{4 , 5, 6}, ` `        ``{10, 8, -12} ` `    ``}; ` ` `  `    ``cout << difference(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// JAVA Code for Find difference between sums ` `// of two diagonals ` `class` `GFG { ` `     `  `    ``public` `static` `int` `difference(``int` `arr[][], ``int` `n) ` `    ``{ ` `        ``// Initialize sums of diagonals ` `        ``int` `d1 = ``0``, d2 = ``0``; ` `      `  `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``for` `(``int` `j = ``0``; j < n; j++) ` `            ``{ ` `                ``// finding sum of primary diagonal ` `                ``if` `(i == j) ` `                    ``d1 += arr[i][j]; ` `      `  `                ``// finding sum of secondary diagonal ` `                ``if` `(i == n - j - ``1``) ` `                    ``d2 += arr[i][j]; ` `            ``} ` `        ``} ` `      `  `        ``// Absolute difference of the sums ` `        ``// across the diagonals ` `        ``return` `Math.abs(d1 - d2); ` `    ``} ` `     `  `    ``/* Driver program to test above function */` `    ``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)); ` `        `  `    ``} ` `  ``} ` `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python3 program to find the difference ` `# between the sum of diagonal. ` `def` `difference(arr, n): ` ` `  `    ``# Initialize sums of diagonals ` `    ``d1 ``=` `0` `    ``d2 ``=` `0` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``for` `j ``in` `range``(``0``, n): ` `         `  `            ``# finding sum of primary diagonal ` `            ``if` `(i ``=``=` `j): ` `                ``d1 ``+``=` `arr[i][j] ` ` `  `            ``# finding sum of secondary diagonal ` `            ``if` `(i ``=``=` `n ``-` `j ``-` `1``): ` `                ``d2 ``+``=` `arr[i][j] ` `         `  `    ``# Absolute difference of the sums ` `    ``# across the diagonals ` `    ``return` `abs``(d1 ``-` `d2); ` ` `  `# Driver Code ` `n ``=` `3` ` `  `arr ``=` `[[``11``, ``2``, ``4``], ` `       ``[``4` `, ``5``, ``6``], ` `       ``[``10``, ``8``, ``-``12``]] ` ` `  `print``(difference(arr, n)) ` `     `  `# This code is contributed  ` `# by ihritik `

## C#

 `// C# Code for find difference between ` `// sums of two diagonals ` `using` `System; ` ` `  `public` `class` `GFG ` `{ ` ` `  `    ``// Function to calculate difference ` `    ``public` `static` `int` `difference(``int``[,] arr, ` `                                 ``int` `n) ` `    ``{ ` `         `  `        ``// Initialize sums of diagonals ` `        ``int` `d1 = 0, d2 = 0; ` `     `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``for` `(``int` `j = 0; j < n; j++) ` `            ``{ ` `                 `  `                ``// finding sum of primary diagonal ` `                ``if` `(i == j) ` `                    ``d1 += arr[i, j]; ` `     `  `                ``// finding sum of secondary diagonal ` `                ``if` `(i == n - j - 1) ` `                    ``d2 += arr[i, j]; ` `            ``} ` `        ``} ` `     `  `        ``// Absolute difference of the ` `        ``// sums across the diagonals ` `        ``return` `Math.Abs(d1 - d2); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `n = 3; ` `         `  `        ``int``[,] arr ={{11, 2, 4}, ` `                     ``{4 , 5, 6}, ` `                     ``{10, 8, -12}}; ` `     `  `        ``Console.Write(difference(arr, n)); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by shiv_bhakt. `

## PHP

 ` `

Output:

```15
```

Time complexity : O(n*n)

We can optimize above solution to work in O(n) using the patterns present in indexes of cells.

## C++

 `// C++ program to find the difference ` `// between the sum of diagonal. ` `#include ` `#define MAX 100 ` `using` `namespace` `std; ` ` `  `int` `difference(``int` `arr[][MAX], ``int` `n) ` `{ ` `    ``// Initialize sums of diagonals ` `    ``int` `d1 = 0, d2 = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``d1 += arr[i][i]; ` `        ``d2 += arr[i][n-i-1]; ` `    ``} ` ` `  `    ``// Absolute difference of the sums ` `    ``// across the diagonals ` `    ``return` `abs``(d1 - d2); ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``int` `n = 3; ` ` `  `    ``int` `arr[][MAX] = ` `    ``{ ` `        ``{11, 2, 4}, ` `        ``{4 , 5, 6}, ` `        ``{10, 8, -12} ` `    ``}; ` ` `  `    ``cout << difference(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// JAVA Code for Find difference between sums ` `// of two diagonals ` ` `  `class` `GFG { ` `     `  `    ``public` `static` `int` `difference(``int` `arr[][], ``int` `n) ` `    ``{ ` `        ``// Initialize sums of diagonals ` `        ``int` `d1 = ``0``, d2 = ``0``; ` `      `  `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``d1 += arr[i][i]; ` `            ``d2 += arr[i][n-i-``1``]; ` `        ``} ` `      `  `        ``// Absolute difference of the sums ` `        ``// across the diagonals ` `        ``return` `Math.abs(d1 - d2); ` `    ``} ` `     `  `    ``/* Driver program to test above function */` `    ``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)); ` `        `  `    ``} ` `  ``} ` `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python3 program to find the difference ` `# between the sum of diagonal. ` `def` `difference(arr, n): ` ` `  `    ``# Initialize sums of diagonals ` `    ``d1 ``=` `0` `    ``d2 ``=` `0` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `        ``d1 ``=` `d1 ``+` `arr[i][i] ` `        ``d2 ``=` `d2 ``+` `arr[i][n ``-` `i ``-` `1``] ` `         `  `    ``# Absolute difference of the sums ` `    ``# across the diagonals ` `    ``return` `abs``(d1 ``-` `d2) ` ` `  `# Driver Code ` `n ``=` `3` ` `  `arr ``=` `[[``11``, ``2``, ``4``], ` `       ``[``4` `, ``5``, ``6``], ` `       ``[``10``, ``8``, ``-``12``]] ` ` `  `print``(difference(arr, n)) ` `     `  `# This code is contributed ` `# by ihritik `

## C#

 `// C# Code for find difference between  ` `// sums of two diagonals ` `using` `System; ` ` `  `public` `class` `GFG ` ` `  `{ ` `     `  `    ``//Function to find difference ` `    ``public` `static` `int` `difference(``int``[,] arr, ` `                                 ``int` `n) ` `    ``{ ` `         `  `        ``// Initialize sums of diagonals ` `        ``int` `d1 = 0, d2 = 0; ` `     `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``d1 += arr[i, i]; ` `            ``d2 += arr[i, n - i - 1]; ` `        ``} ` `     `  `        ``// Absolute difference of the sums ` `        ``// across the diagonals ` `        ``return` `Math.Abs(d1 - d2); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `n = 3; ` `         `  `        ``int``[,] arr ={{11, 2, 4}, ` `                     ``{4 , 5, 6}, ` `                     ``{10, 8, -12}}; ` `     `  `        ``Console.Write(difference(arr, n)); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by shiv_bhakt. `

## PHP

 ` `

Output:

```15
```

Time complexity : O(n)

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.