Toeplitz Matrix

 

This matrix is named after Otto Toeplitz and it contains the same element in each diagonal running from left to right. The daily coding problem goes as follows

In linear algebra, a Toeplitz matrix is one in which the elements on any given diagonal from top left to bottom right are identical.

Here is an example:

1 2 3 4 8
5 1 2 3 4
4 5 1 2 3
7 4 5 1 2

Write a program to determine whether a given input is a Toeplitz matrix.

Understanding the Problem

If you haven't heard of Toeplitz Matrix (like me), the problem can be confusing until you pay close attention to all elements in the matrix and notice the similar elements in each diagonal running left to right


Solving the problem

One way to solve this would be to traverse each element row by row, with the exception of elements in the last row and column. For each element, confirm that the element diagonally below it matches. If the traversal continues till the end we have a Toeplitz Matrix.

Another solution would be to traverse the top row followed by the left column. For each element traverse the diagonal that it initiates and confirm that each number encountered matches.