![]() |
VOOZH | about |
Given a square matrix mat[][] of order n, check if it is a Toeplitz Matrix.
Note: A Toeplitz matrix - also called a diagonal-constant matrix - is a matrix where elements of every individual descending diagonal are same from left to right. Equivalently, for any entry mat[i][j], it is same as mat[i-1][j-1] or mat[i-2][j-2] and so on. We can get a better idea using the following image, all same colored cells should have same values.
👁 ImageExamples:
Input: mat[][] = [ [6, 7, 8]
[4, 6, 7]
[1, 4, 6] ]
Output: Yes
Explanation: All the diagonals of the given matrix are [6, 6, 6], [7, 7], [8], [4, 4], [1]. For every diagonal, as all the elements are same, the given matrix is Toeplitz Matrix.Input: mat[][] = [ [6, 3, 8]
[4, 9, 7]
[1, 4, 6] ]
Output: No
Explanation: The primary diagonal elements of the given matrix are [6, 9, 6]. As the diagonal elements are not same, the given matrix is not Toeplitz Matrix.
Table of Content
Traverse every downward-sloping diagonal in the matrix by using each element in the first row and each element in the first column as a starting point, and verify that every element along that diagonal matches the value at its head.
Follow the below given steps:
true
The core idea is to examine each cell, starting from the second row and second column. We compare each cell's value with its top-left neighbor. If a mismatch is found, the matrix is not Toeplitz, and we can stop. If the entire matrix is checked without any mismatches, all diagonals are constant.
Follow the below given steps:
true