Given a square matrix, the task is to find the element of the matrix where the right and the left diagonal of this square matrix converge.

**Example:**

Input:n = 5, matrix = [ 1 2 3 4 5 5 6 7 8 6 9 5 6 8 7 2 3 5 6 8 1 2 3 4 5 ]Output:6Input:n = 4, matrix = [ 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 1 ]Output:NULL Here there no converging element at all. Hence the answer isnull.

**Approach:**

- If the number of rows and column of the matrix are even, then we just print NULL because there would be no converging element in the case of even number of rows and column.
- If the number of rows and column of the matrix are odd, find the mid-value of n as
mid = n/2

- The arr[mid][mid] itself is the converging diagonal element.

Below is the implementation of the above approach:

## C++

`// C++ program to find the converging element ` `// of the diagonals in a square matrix ` ` ` `#include <malloc.h> ` `#include <stdio.h> ` `#include <stdlib.h> ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `a[][5] = { { 1, 2, 3, 4, 5 }, ` ` ` `{ 5, 6, 7, 8, 6 }, ` ` ` `{ 9, 5, 6, 8, 7 }, ` ` ` `{ 2, 3, 5, 6, 8 }, ` ` ` `{ 1, 2, 3, 4, 5 } }; ` ` ` ` ` `int` `convergingele, mid; ` ` ` `int` `i, j; ` ` ` ` ` `// If n is even, then convergence ` ` ` `// element will be null. ` ` ` `if` `(n % 2 == 0) { ` ` ` `printf` `(` `"NULL\n"` `); ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` `// finding the mid ` ` ` `mid = n / 2; ` ` ` ` ` `// finding the converging element ` ` ` `convergingele = a[mid][mid]; ` ` ` ` ` `printf` `(` `"%d\n"` `, convergingele); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the converging element ` `// of the diagonals in a square matrix ` `class` `GFG ` `{ ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `int` `a[][] = {{` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `}, ` ` ` `{` `5` `, ` `6` `, ` `7` `, ` `8` `, ` `6` `}, ` ` ` `{` `9` `, ` `5` `, ` `6` `, ` `8` `, ` `7` `}, ` ` ` `{` `2` `, ` `3` `, ` `5` `, ` `6` `, ` `8` `}, ` ` ` `{` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `}}; ` ` ` ` ` `int` `convergingele, mid; ` ` ` `int` `i, j; ` ` ` ` ` `// If n is even, then convergence ` ` ` `// element will be null. ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `System.out.printf(` `"NULL\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// finding the mid ` ` ` `mid = n / ` `2` `; ` ` ` ` ` `// finding the converging element ` ` ` `convergingele = a[mid][mid]; ` ` ` ` ` `System.out.printf(` `"%d\n"` `, convergingele); ` ` ` `} ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the converging element ` `# of the diagonals in a square matrix ` ` ` `# Driver code ` `n ` `=` `5` `a ` `=` `[[ ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `], ` ` ` `[ ` `5` `, ` `6` `, ` `7` `, ` `8` `, ` `6` `], ` ` ` `[ ` `9` `, ` `5` `, ` `6` `, ` `8` `, ` `7` `], ` ` ` `[ ` `2` `, ` `3` `, ` `5` `, ` `6` `, ` `8` `], ` ` ` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `]] ` ` ` `# If n is even, then convergence ` `# element will be null. ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `print` `(` `"NULL"` `) ` `else` `: ` ` ` ` ` `# finding the mid ` ` ` `mid ` `=` `n ` `/` `/` `2` ` ` ` ` `# finding the converging element ` ` ` `convergingele ` `=` `a[mid][mid] ` ` ` ` ` `print` `(convergingele) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# iprogram to find the converging element ` `// of the diagonals in a square matrix ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `int` `[,]a = {{1, 2, 3, 4, 5}, ` ` ` `{5, 6, 7, 8, 6}, ` ` ` `{9, 5, 6, 8, 7}, ` ` ` `{2, 3, 5, 6, 8}, ` ` ` `{1, 2, 3, 4, 5}}; ` ` ` ` ` `int` `convergingele, mid; ` ` ` ` ` `// If n is even, then convergence ` ` ` `// element will be null. ` ` ` `if` `(n % 2 == 0) ` ` ` `{ ` ` ` `Console.Write(` `"NULL\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// finding the mid ` ` ` `mid = n / 2; ` ` ` ` ` `// finding the converging element ` ` ` `convergingele = a[mid,mid]; ` ` ` ` ` `Console.Write(` `"{0}\n"` `, convergingele); ` ` ` `} ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find smallest and largest element from square matrix diagonals
- Sum of both diagonals of a spiral odd-order square matrix
- Row-wise common elements in two diagonals of a square matrix
- Swap major and minor diagonals of a square matrix
- Sum of all parts of a square Matrix divided by its diagonals
- Find the product of sum of two diagonals of a square Matrix
- Return an array of anti-diagonals of given N*N square matrix
- Construct a Matrix whose sum of diagonals for each square submatrix is even
- Create matrix whose sum of diagonals in each sub matrix is even
- Center element of matrix equals sums of half diagonals
- Finding the maximum square sub-matrix with all equal elements
- Program to Interchange Diagonals of Matrix
- Efficiently compute sums of diagonals of a matrix
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Program to print the Diagonals of a Matrix
- Program to print the Diagonals of a Matrix in O(N) time
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Check if a matrix contains a square submatrix with 0 as boundary element
- Smallest element from all square submatrices of size K from a given Matrix

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.