Problem title
Difficulty
Avg time to solve

Kevin and the tower of coins
Moderate
25 mins
Distribution of Money
Easy
15 mins
Letter Case Permutation
Moderate
15 mins
Product of the Last K Numbers
Easy
15 mins
Kevin And The Number On A Dice
Easy
10 mins
Digit Count In Range
Moderate
25 mins
Maximum Distinct Blocks
Easy
10 mins
Flatten 2D array
Easy
20 mins
Nearest Pallindrome
Moderate
30 mins
Find a Corresponding Node of a Binary Tree in a Clone of That Tree
Easy
20 mins

Toeplitz Matrix

Difficulty: EASY
Contributed By
Avg. time to solve
25 min
Success Rate
80%

Problem Statement

You are given a matrix of size N * M. You have to find out whether the matrix is Toeplitz or not.

A Matrix is said to be Toeplitz if every diagonal from top-left to bottom-right has the same elements.

You are given the matrix ‘MAT’ of size N * M.Your task is to find out whether it is Toeplitz or not.

For Example
 The given matrix is Toeplitz:

altImage

Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.

The first line of each test case contains two integers,' N’ and ‘M’ denoting the number of rows and columns.

The next line of each test case has ‘N’ lines that have M values corresponding to the matrix ‘MAT’.
Output Format:
For each test case, print ‘YES’ or ‘NO’ whether the matrix is Toeplitz or not.

 Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 1000.
1 <= M <= 1000.

Time limit: 1 sec
Sample Input 1:
2
3 3
2 1 3
1 2 1
5 1 2
4 3
0 0 0
7 0 1
1 7 0
0 1 7
Sample Output 1:
YES
NO
Explanation of sample input 1:
For the first test case,

altImage

All the diagonals of the given matrix has same values.Hence, the given matrix is Toeplitz.

For the second test case:
The diagonal with cells (0,1) and  (1,2) does not have the same elements. Hence, the matrix is not Toeplitz.
Sample Input 2:
2
4 3
10 10 10 
4 10 10 
2 10 10 
4 4 8 
3 4
5 10 4 10 
5 5 10 4 
9 5 5 10
Sample Output 2:
NO
YES
Reset Code
Full screen
copy-code
Console