# Find the product of sum of two diagonals of a square Matrix

Given a square matrix **mat** consisting of integers of size **NxN**, the task is to calculate the product between the sums of its diagonal.

**Examples:**

Input:mat[][] = {{5, 8, 1}, {5, 10, 3}, {-6, 17, -9}}Output:30 Sum of primary diagonal = 5 + 10 + (-9) = 6. Sum of secondary diagonal = 1 + 10 + (-6) = 5. Product = 6 * 5 = 30.Input:mat[][] = {{22, -8, 11}, {55, 87, -1}, {-61, 69, 19}}Output:4736

**Naive approach:** Traverse the entire matrix and find the diagonal elements. Calculate the sums across the two diagonals of a square matrix. Then, just take the product of the two sums obtained.

**Time complexity:** O(N^{2})

**Naive approach:** Traverse just the diagonal elements instead of the entire matrix by observing the pattern in the indices of the diagonal elements.

Below is the implementation of this approach:

## CPP

`// C++ program to find the product ` `// of the sum of diagonals. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the product ` `// of the sum of diagonals. ` `long` `long` `product(vector<vector<` `int` `>> &mat, ` `int` `n) ` `{ ` ` ` `// Initialize sums of diagonals ` ` ` `long` `long` `d1 = 0, d2 = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `d1 += mat[i][i]; ` ` ` `d2 += mat[i][n - i - 1]; ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `1LL * d1 * d2; ` `} ` ` ` `// Driven code ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `>> mat = {{ 5, 8, 1}, ` ` ` `{ 5, 10, 3}, ` ` ` `{ -6, 17, -9}}; ` ` ` ` ` `int` `n = mat.size(); ` ` ` ` ` `// Function call ` ` ` `cout << product(mat, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the product ` `// of the sum of diagonals. ` ` ` ` ` `class` `GFG{ ` ` ` `// Function to find the product ` `// of the sum of diagonals. ` `static` `long` `product(` `int` `[][]mat, ` `int` `n) ` `{ ` ` ` `// Initialize sums of diagonals ` ` ` `long` `d1 = ` `0` `, d2 = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `d1 += mat[i][i]; ` ` ` `d2 += mat[i][n - i - ` `1` `]; ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `1L * d1 * d2; ` `} ` ` ` `// Driven code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[][]mat = {{ ` `5` `, ` `8` `, ` `1` `}, ` ` ` `{ ` `5` `, ` `10` `, ` `3` `}, ` ` ` `{ -` `6` `, ` `17` `, -` `9` `}}; ` ` ` ` ` `int` `n = mat.length; ` ` ` ` ` `// Function call ` ` ` `System.out.print(product(mat, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the product ` `# of the sum of diagonals. ` ` ` `# Function to find the product ` `# of the sum of diagonals. ` `def` `product(mat,n): ` ` ` ` ` `# Initialize sums of diagonals ` ` ` `d1 ` `=` `0` ` ` `d2 ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `d1 ` `+` `=` `mat[i][i] ` ` ` `d2 ` `+` `=` `mat[i][n ` `-` `i ` `-` `1` `] ` ` ` ` ` `# Return the answer ` ` ` `return` `d1 ` `*` `d2 ` ` ` ` ` `# Driven code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `mat ` `=` `[[` `5` `, ` `8` `, ` `1` `], ` ` ` `[` `5` `, ` `10` `, ` `3` `], ` ` ` `[` `-` `6` `, ` `17` `, ` `-` `9` `]] ` ` ` ` ` `n ` `=` `len` `(mat) ` ` ` ` ` `# Function call ` ` ` `print` `(product(mat, n)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the product ` `// of the sum of diagonals. ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the product ` `// of the sum of diagonals. ` `static` `long` `product(` `int` `[,]mat, ` `int` `n) ` `{ ` ` ` `// Initialize sums of diagonals ` ` ` `long` `d1 = 0, d2 = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `d1 += mat[i, i]; ` ` ` `d2 += mat[i, n - i - 1]; ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `1L * d1 * d2; ` `} ` ` ` `// Driven code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[,]mat = {{ 5, 8, 1}, ` ` ` `{ 5, 10, 3}, ` ` ` `{ -6, 17, -9}}; ` ` ` ` ` `int` `n = mat.GetLength(0); ` ` ` ` ` `// Function call ` ` ` `Console.Write(product(mat, n)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

30

**Time complexity:** O(N)

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.

## Recommended Posts:

- Find smallest and largest element from square matrix diagonals
- Sum of all parts of a square Matrix divided by its diagonals
- Sum of both diagonals of a spiral odd-order square matrix
- Finding the converging element of the diagonals in a square matrix
- Row-wise common elements in two diagonals of a square matrix
- Swap major and minor diagonals of a square matrix
- Return an array of anti-diagonals of given N*N square matrix
- Product of middle row and column in an odd square matrix
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Find a Square Matrix such that sum of elements in every row and column is K
- Find Maximum side length of square in a Matrix
- Find number of square of area Z which can be built in a matrix having blocked regions
- Program to Interchange Diagonals of Matrix
- Program to print the Diagonals of a Matrix
- Program to find the Product of diagonal elements of a matrix
- Program to print the Diagonals of a Matrix in O(N) time
- Efficiently compute sums of diagonals of a matrix
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Center element of matrix equals sums of half diagonals
- Check if matrix can be converted to another matrix by transposing square sub-matrices

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.