Row-wise common elements in two diagonals of a square matrix Last Updated : 19 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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]. Implementation: 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; } 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. 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. 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. 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 ?> JavaScript <script> // Javascript program to find common elements in // two diagonals. let MAX = 100; // Returns count of row wise same // elements in two diagonals of // mat[n][n] function countCommon(mat, n) { let res = 0; for (let i = 0; i < n; i++) if (mat[i][i] == mat[i][n - i - 1]) res++; return res; } // Driver Code let mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; document.write(countCommon(mat, 3)); // This code is contributed by subham348. </script> Output1 Time Complexity: O(n). Auxiliary Space: O(1). Comment More infoAdvertise with us Next Article Row-wise common elements in two diagonals of a square matrix H Himanshu Ranjan Improve Article Tags : Pattern Searching Searching Matrix DSA Practice Tags : MatrixPattern SearchingSearching Similar Reads Find distinct elements common to all rows of a matrix Given a n x n matrix. The problem is to find all the distinct elements common to all rows of the matrix. The elements can be printed in any order. Examples: Input : mat[][] = { {2, 1, 4, 3}, {1, 2, 3, 2}, {3, 6, 2, 3}, {5, 2, 5, 3} } Output : 2 3 Input : mat[][] = { {12, 1, 14, 3, 16}, {14, 2, 1, 3, 15+ min read Squares of Matrix Diagonal Elements Given an integer matrix mat[][] of odd dimensions, the task is to find the square of the elements of the Primary and Secondary diagonals.The Primary and Secondary diagonal matrix explanation is given below:Primary Diagonal Elements of a Matrix: The Primary Diagonal Elements are the ones that occur f 11 min read Print all the sub diagonal elements of the given square matrix Given a square matrix mat[][] of size n * n. The task is to print all the elements which lie on the sub-diagonal of the given matrix.Examples: Input: mat[][] = { {1, 2, 3}, {3, 3, 4, }, {2, 4, 6}} Output: 3 4Input: mat[][] = { {1, 2, 3, 4}, {3, 3, 4, 4}, {2, 4, 6, 3}, {1, 1, 1, 3}} Output: 3 4 1 Rec 7 min read Print all the super diagonal elements of the given square matrix Given a square matrix mat[][] of size n * n. The task is to print all the elements which lie on the super-diagonal of the given matrix.Examples: Input: mat[][] = { {1, 2, 3}, {3, 3, 4, }, {2, 4, 6}} Output: 2 4Input: mat[][] = { {1, 2, 3, 4}, {3, 3, 4, 4}, {2, 4, 6, 3}, {1, 1, 1, 3}} Output: 2 4 3 A 4 min read Check if two elements of a matrix are on the same diagonal or not Given a matrix mat[][], and two integers X and Y, the task is to check if X and Y are on the same diagonal of the given matrix or not. Examples: Input: mat[][]= {{1, 2}. {3, 4}}, X = 1, Y = 4 Output: YesExplanation:Both X and Y lie on the same diagonal. Input: mat[][]= {{1, 2}. {3, 4}}, X = 2, Y = 4 7 min read Largest square sub-matrix with equal row, column, and diagonal sum Given a matrix mat[][] of dimensions N*M, the task is to find the size of the largest square submatrix such that the sum of all rows, columns, diagonals in that submatrix are equal.Examples:Input: N = 3, M = 4, mat[][] = [[5, 1, 3, 1], [9, 3, 3, 1], [1, 3, 3, 8]]Output: 2Explanation:The submatrix wh 11 min read Program to find the Product of diagonal elements of a matrix Given an N * N matrix, the task is to find the product of the elements of left and right diagonal. Examples: Input: arr[] = 1 2 3 4 5 6 7 8 9 7 4 2 2 2 2 1Output: 9408Explanation:Product of left diagonal = 1 * 4 * 6 * 1 = 24Product of right diagonal = 4 * 7 * 7 * 2 = 392Total product = 24 * 392 = 94 7 min read Common elements in all rows of a given matrix Given an m x n matrix, find all common elements present in all rows in O(mn) time and one traversal of matrix.Example: Input:mat[4][5] = {{1, 2, 1, 4, 8}, {3, 7, 8, 5, 1}, {8, 7, 7, 3, 1}, {8, 1, 2, 7, 9}, };Output: 1 8 or 8 18 and 1 are present in all rows.A simple solution is to consider every ele 7 min read Center element of matrix equals sums of half diagonals Given a matrix of odd order i.e(5*5). Task is to check if the center element of the matrix is equal to the individual sum of all the half diagonals. Examples: Input : mat[][] = { 2 9 1 4 -2 6 7 2 11 4 4 2 9 2 4 1 9 2 4 4 0 2 4 2 5 } Output : Yes Explanation : Sum of Half Diagonal 1 = 2 + 7 = 9 Sum o 7 min read Find a common element in all rows of a given row-wise sorted matrix Given a matrix where every row is sorted in increasing order. Write a function that finds and returns a common element in all rows. If there is no common element, then returns -1. Example: Input: mat[4][5] = { {1, 2, 3, 4, 5}, {2, 4, 5, 8, 10}, {3, 5, 7, 9, 11}, {1, 3, 5, 7, 9}, };Output: 5A O(m*n*n 15+ min read Like