#C7701. Toeplitz Matrix Checker
Toeplitz Matrix Checker
Toeplitz Matrix Checker
You are given a 2D matrix of integers. A matrix is said to be a Toeplitz matrix if every diagonal from top-left to bottom-right has the same element. In other words, for every element \(A_{i,j}\) (with \(i > 0\) and \(j > 0\)), it must hold that \(A_{i,j} = A_{i-1,j-1}\).
Your task is to determine whether the given matrix is Toeplitz.
Definition
A matrix \(A\) of dimensions \(n \times m\) is Toeplitz if for every valid index \(i, j\) (where \(1 \leq i < n\) and \(1 \leq j < m\)), the following condition holds:
[ A_{i,j} = A_{i-1,j-1} ]
If the matrix is empty or has only one row or one column, it is considered Toeplitz by definition.
inputFormat
The input is given via standard input. The first line contains two integers \(n\) and \(m\) denoting the number of rows and columns of the matrix, respectively. This is followed by \(n\) lines each containing \(m\) integers separated by spaces, representing the matrix.
If no input is provided, consider the matrix as empty and output True
.
outputFormat
Output a single line to standard output containing either True
if the matrix is Toeplitz, or False
otherwise.
3 3
1 2 3
4 1 2
7 4 1
True