using System;
// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements
public class Program
{
private static bool isDiagonalSameValue(int[][] matrix, int i, int j) {
int rows = matrix.Length;
int cols = matrix[0].Length;
int value = matrix[i][j];
while (++i < rows && ++j < cols) {
Console.WriteLine(i + " " + j + " - " + matrix[i][j]);
if (matrix[i][j] != value) {
return false;
}
}
Console.WriteLine("-------");
return true;
}
private static bool isToeplitz(int[][] matrix) {
int rows = matrix.Length;
int cols = matrix[0].Length;
for (int i = 0; i < rows - 1; i++) {
if (!isDiagonalSameValue(matrix, 0, i)) {
return false;
}
}
for (int j = 1; j < cols; j++) {
if (!isDiagonalSameValue(matrix, j, 0)) {
return false;
}
}
return true;
}
public static void Main(string[] args)
{
int[][] matrix = new int[][]
{
new int[] {2, 7, 9, 8},
new int[] {4, 2, 7, 9},
new int[] {3, 4, 2, 7},
new int[] {0, 3, 4, 2},
new int[] {6, 0, 3, 4}
};
if (isToeplitz(matrix)) {
Console.Write("Matrix is a Toeplitz");
}
else {
Console.Write("Matrix is not a Toeplitz");
}
}
}
/*
run:
1 1 - 2
2 2 - 2
3 3 - 2
-------
1 2 - 7
2 3 - 7
-------
1 3 - 9
-------
-------
2 1 - 4
3 2 - 4
4 3 - 4
-------
3 1 - 3
4 2 - 3
-------
4 1 - 0
-------
Matrix is a Toeplitz
*/