# Row-wise common elements in two diagonals of a square matrix

Given a square matrix, find out count of numbers that are same in same row and same in both primary and secondary diagonals.

**Examples : **

Input : 1 2 1 4 5 2 0 5 1 Output : 2 Primary diagonal is 1 5 1 Secondary diagonal is 1 5 0 Two elements (1 and 5) match in two diagonals and same. Input : 1 0 0 0 1 0 0 0 1 Output : 1 Primary diagonal is 1 1 1 Secondary diagonal is 0 1 0 Only one element is same.

We can achieve this in O(n) time, O(1) space and only one traversal. We can find current element in i-th row of primary diagonal as mat[i][i] and i-th element of secondary diagonal as mat[i][n-i-1].

## C++

`// CPP program to find common elements in ` `// two diagonals. ` `#include <iostream> ` `#define MAX 100 ` `using` `namespace` `std; ` ` ` `// Returns count of row wise same ` `// elements in two diagonals of ` `// mat[n][n] ` `int` `countCommon(` `int` `mat[][MAX], ` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` `for` `(` `int` `i=0;i<n;i++) ` ` ` `if` `(mat[i][i] == mat[i][n-i-1]) ` ` ` `res++; ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `mat[][MAX] = {{1, 2, 3}, ` ` ` `{4, 5, 6}, ` ` ` `{7, 8, 9}}; ` ` ` `cout << countCommon(mat, 3); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find common ` `// elements in two diagonals. ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `int` `MAX = ` `100` `; ` ` ` ` ` `// Returns count of row wise same elements ` ` ` `// in two diagonals of mat[n][n] ` ` ` `static` `int` `countCommon(` `int` `mat[][], ` `int` `n) ` ` ` `{ ` ` ` `int` `res = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `if` `(mat[i][i] == mat[i][n - i - ` `1` `]) ` ` ` `res++; ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[])` `throws` `IOException ` ` ` `{ ` ` ` `int` `mat[][] = {{` `1` `, ` `2` `, ` `3` `}, ` ` ` `{` `4` `, ` `5` `, ` `6` `}, ` ` ` `{` `7` `, ` `8` `, ` `9` `}}; ` ` ` `System.out.println(countCommon(mat, ` `3` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anshika Goyal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find common ` `# elements in two diagonals. ` ` ` `Max` `=` `100` ` ` `# Returns count of row wise same ` `# elements in two diagonals of ` `# mat[n][n] ` `def` `countCommon(mat, n): ` ` ` `res ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `if` `mat[i][i] ` `=` `=` `mat[i][n` `-` `i` `-` `1` `] : ` ` ` `res ` `=` `res ` `+` `1` ` ` `return` `res ` ` ` `# Driver Code ` `mat ` `=` `[[` `1` `, ` `2` `, ` `3` `], ` ` ` `[` `4` `, ` `5` `, ` `6` `], ` ` ` `[` `7` `, ` `8` `, ` `9` `]] ` ` ` `print` `(countCommon(mat, ` `3` `)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find common ` `// elements in two diagonals. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns count of row wise same ` ` ` `// elements in two diagonals of ` ` ` `// mat[n][n] ` ` ` `static` `int` `countCommon(` `int` `[,]mat, ` `int` `n) ` ` ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(mat[i,i] == mat[i,n - i - 1]) ` ` ` `res++; ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[,]mat = {{1, 2, 3}, ` ` ` `{4, 5, 6}, ` ` ` `{7, 8, 9}}; ` ` ` `Console.WriteLine(countCommon(mat, 3)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find common ` `// elements in two diagonals. ` `$MAX` `= 100; ` ` ` `// Returns count of row wise ` `// same elements in two ` `// diagonals of mat[n][n] ` `function` `countCommon(` `$mat` `, ` `$n` `) ` `{ ` ` ` `global` `$MAX` `; ` ` ` `$res` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$mat` `[` `$i` `][` `$i` `] == ` `$mat` `[` `$i` `][` `$n` `- ` `$i` `- 1]) ` ` ` `$res` `++; ` ` ` `return` `$res` `; ` `} ` ` ` `// Driver Code ` `$mat` `= ` `array` `(` `array` `(1, 2, 3), ` ` ` `array` `(4, 5, 6), ` ` ` `array` `(7, 8, 9)); ` `echo` `countCommon(` `$mat` `, 3); ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1

This article is contributed by **Himanshu Ranjan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Sum of both diagonals of a spiral odd-order 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
- Swap major and minor diagonals of a square matrix
- Finding the converging element of the diagonals in a square matrix
- Find smallest and largest element from square matrix diagonals
- Sort Matrix in alternating ascending and descending order rowwise
- Common elements in all rows of a given matrix
- Find distinct elements common to all rows of a matrix
- How to access elements of a Square Matrix
- Print all the sub diagonal elements of the given square matrix
- Find a Square Matrix such that sum of elements in every row and column is K
- Print all the super diagonal elements of the given square matrix
- Finding the maximum square sub-matrix with all equal elements
- Program to Interchange Diagonals of Matrix
- Program to print the Diagonals of a Matrix
- Efficiently compute sums of diagonals of a matrix
- Program to print the Diagonals of a Matrix in O(N) time
- Number of cells in the right and left diagonals passing through (x, y) in a matrix