Toeplitz matrix | Practice | GeeksforGeeks

A Toeplitz (or diagonal-constant) matrix is a matrix in which each descending diagonal from left to right is constant, i.e., all elements in a diagonal are same.
Given a matrix A of order N X M your task is to complete the function isToeplitz which returns true if the matrix is Toeplitz otherwise returns false.
Example 1: 
Input: 3 3 6 7 8 4 6 7 1 4 6 
Output: 1
Explanation:
The test case represents a 3x3 matrix which looks like 
 6 7 8 
 4 6 7 
 1 4 6
Output: 1(True) as values in all diagonals are same.
Example 2:
Input:
2 3
1 2 3 4 5 6
Output: 0
Explanation: 
Matrix of order 2x3 will be 
     1 2 3
     4 5 6
Output: 0(False) as values in all diagonals 
are not same.

Your task: You don't need to read input or print anything. Your task is to complete the function isToepliz() which takes the matrix and size of matrix as input parameters and returns 1 if the matrix is a Toepliz matrix , otherwise return 0. 
Constraints: 
1<=N,M<=40 
1<=A[i][j]<=100
1<=i<=N,1<=j<=M
Expected time complexity:O(nxn)
Expected space complexity:O(1)
0
abhayjcs202 months ago
time taken -0.00
for(int i=0;i<N-1;i++){         for(int j=0;j<M-1;j++){             if(A[i][j]!=A[i+1][j+1]){                 return false;             }         }              }     return true;
0
cshubham4393 months ago
int num = mat[0][0];       for(int i = 0; i<N; i++){           for(int j = 0; j<M; j++){               if(i==j){                   if(num!=mat[i][j]){                       return false;                   }               }           }       }       return true;
0
keshrishivam41143 months ago
Python code
 
def isToepliz(lis, n, m):      for i in range(n-1):       for j in range(m-1):           if lis[i][j]!=lis[i+1][j+1]:               return 0   return 1
0
bhoomikasahu00104 months ago
bool isToepliz(int A[MAX][MAX],int N,int M){   for(int i=0; i<N-1; i++){       for(int j=0; j<M-1; j++){           if(A[i][j] != A[i+1][j+1])               return 0;       }   }   return 1;}
0
pankajkumarravi6 months ago
********************* java solution ***************
 
 boolean isToepliz(int mat[][], int N, int M) {       int i =0,j=0;       int resp = mat[i][j];       while (++i<N && ++j <M){                       // found mismatch                           if (resp != mat[i][j])                               return false;       }               return true;
 }
0
mrshobhit7 months ago
Python 3:
Execution Time - 0.0 Sec
 
 
def isToepliz(lis, n, m):
    #code here
    for i in range(n):
        for j in range(m):
            if i==0 or j==0:
                continue
            if lis[i][j] != lis[i-1][j-1]:
                return False
    return True
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.