Find if given matrix is Toeplitz or not | C # program to check whether given matrix is a Toeplitz matrix or not ; row = number of rows col = number of columns ; HashMap to store key , value pairs ; If key value exists in the hashmap , ; We check whether the current value stored in this key matches to element at current index or not . If not , return false ; Else we put key , value pair in hashmap ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static bool isToeplitz ( int [ , ] matrix ) { int row = matrix . GetLength ( 0 ) ; int col = matrix . GetLength ( 1 ) ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { int key = i - j ; if ( map . ContainsKey ( key ) ) { if ( map [ key ] != matrix [ i , j ] ) return false ; } else { map . Add ( i - j , matrix [ i , j ] ) ; } } } return true ; } static void Main ( ) { int [ , ] matrix = { { 12 , 23 , - 32 } , { - 20 , 12 , 23 } , { 56 , - 20 , 12 } , { 38 , 56 , - 20 } } ; string result = ( isToeplitz ( matrix ) ) ? " Yes " : " No " ; Console . WriteLine ( result ) ; } }