Toeplitz Matrix or Not

Last Updated : 10 Apr, 2026

Given a square matrix mat[][] of order n, check if it is a Toeplitz Matrix.

Note: A Toeplitz matrix - also called a diagonal-constant matrix - is a matrix where elements of every individual descending diagonal are same from left to right. Equivalently, for any entry mat[i][j], it is same as mat[i-1][j-1] or mat[i-2][j-2] and so on. We can get a better idea using the following image, all same colored cells should have same values.

Examples:

Input: mat[][] = [ [6, 7, 8]
[4, 6, 7]
[1, 4, 6] ]
Output: Yes
Explanation: All the diagonals of the given matrix are [6, 6, 6], [7, 7], [8], [4, 4], [1]. For every diagonal, as all the elements are same, the given matrix is Toeplitz Matrix.

Input: mat[][] = [ [6, 3, 8]
[4, 9, 7]
[1, 4, 6] ]
Output: No
Explanation: The primary diagonal elements of the given matrix are [6, 9, 6]. As the diagonal elements are not same, the given matrix is not Toeplitz Matrix.

Try It Yourself
redirect icon

[Expected Approach - 1] - Checking Each Diagonal - O(n × n) Time and O(1) Space

Traverse every downward-sloping diagonal in the matrix by using each element in the first row and each element in the first column as a starting point, and verify that every element along that diagonal matches the value at its head.

Follow the below given steps:

  • Let n = mat.size() and m = mat[0].size().
  • Check all diagonals starting from the first row and first column using checkDiagonal; if any call returns false, return false.
  • If all calls to checkDiagonal succeed, return true.
  • In checkDiagonal(mat, x, y), compare mat[i][j] to mat[x][y] for each i = x+1, j = y+1 while i < n && j < m; return false on the first mismatch, otherwise return true after reaching the edge.
C++
#include <iostream>
#include<vector>
using namespace std;

// function to check if diagonal elements are same
bool checkDiagonal(vector<vector<int>> &mat, int x, int y) {
    int n = mat.size(), m = mat[0].size();

    for(int i = x + 1, j = y + 1; 
            i < n && j < m; i++, j++) {
        if(mat[i][j] != mat[x][y])
            return false;
    }
    return true;
}

// Function to check whether given
// matrix is toeplitz matrix or not
bool isToeplitz(vector<vector<int>> &mat) {
    int n = mat.size(), m = mat[0].size();

    // check each descending diagonal starting from
    // first row and first column of the matrix
    for(int i = 0; i < m; i++)
        if(!checkDiagonal(mat, 0, i))
            return false;

    for(int i = 0; i < n; i++) 
        if(!checkDiagonal(mat, i, 0))
            return false;
    
    return true;
}

int main() {
    vector<vector<int>> mat = {
        {6, 7, 8},
        {4, 6, 7},
        {1, 4, 6}
    };
    if(isToeplitz(mat)) {
        cout << "true";
    } else {
        cout << "false";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // function to check if diagonal elements are same
    static boolean checkDiagonal(int[][] mat, int x, int y) {
        int n = mat.length, m = mat[0].length;

        for(int i = x + 1, j = y + 1; 
                i < n && j < m; i++, j++) {
            if(mat[i][j] != mat[x][y])
                return false;
        }
        return true;
    }

    // Function to check whether given
    // matrix is toeplitz matrix or not
    static boolean isToeplitz(int[][] mat) {
        int n = mat.length, m = mat[0].length;

        // check each descending diagonal starting from
        // first row and first column of the matrix

        for(int i = 0; i < m; i++)
            if(!checkDiagonal(mat, 0, i))
                return false;

        for(int i = 0; i < n; i++) 
            if(!checkDiagonal(mat, i, 0))
                return false;

        return true;
    }

    public static void main(String[] args) {
        int[][] mat = {
            {6, 7, 8},
            {4, 6, 7},
            {1, 4, 6}
        };

        if(isToeplitz(mat)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# function to check if diagonal elements are same
def checkDiagonal(mat, x, y):
    n, m = len(mat), len(mat[0])

    i, j = x + 1, y + 1
    while i < n and j < m:
        if mat[i][j] != mat[x][y]:
            return False
        i += 1
        j += 1
    return True

# Function to check whether given
# matrix is toeplitz matrix or not
def isToeplitz(mat):
    n, m = len(mat), len(mat[0])

    # check each descending diagonal starting from
    # first row and first column of the matrix

    for i in range(m):
        if not checkDiagonal(mat, 0, i):
            return False

    for i in range(n):
        if not checkDiagonal(mat, i, 0):
            return False

    return True

if __name__ == "__main__":
    mat = [
        [6, 7, 8],
        [4, 6, 7],
        [1, 4, 6]
    ]
    if isToeplitz(mat):
        print("true")
    else:
        print("false")
C#
using System;

class GfG {

    // function to check if diagonal elements are same
    static bool checkDiagonal(int[,] mat, int x, int y) {
        int n = mat.GetLength(0), m = mat.GetLength(1);

        for(int i = x + 1, j = y + 1; 
                i < n && j < m; i++, j++) {
            if(mat[i, j] != mat[x, y])
                return false;
        }
        return true;
    }

    // Function to check whether given
    // matrix is toeplitz matrix or not
    static bool isToeplitz(int[,] mat) {
        int n = mat.GetLength(0), m = mat.GetLength(1);

        // check each descending diagonal starting from
        // first row and first column of the matrix

        for(int i = 0; i < m; i++)
            if(!checkDiagonal(mat, 0, i))
                return false;

        for(int i = 0; i < n; i++) 
            if(!checkDiagonal(mat, i, 0))
                return false;

        return true;
    }

    static void Main() {
        int[,] mat = {
            {6, 7, 8},
            {4, 6, 7},
            {1, 4, 6}
        };

        if(isToeplitz(mat)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// function to check if diagonal elements are same
function checkDiagonal(mat, x, y) {
    let n = mat.length, m = mat[0].length;

    for(let i = x + 1, j = y + 1; 
            i < n && j < m; i++, j++) {
        if(mat[i][j] !== mat[x][y])
            return false;
    }
    return true;
}

// Function to check whether given
// matrix is toeplitz matrix or not
function isToeplitz(mat) {
    let n = mat.length, m = mat[0].length;

    // check each descending diagonal starting from
    // first row and first column of the matrix

    for(let i = 0; i < m; i++)
        if(!checkDiagonal(mat, 0, i))
            return false;

    for(let i = 0; i < n; i++) 
        if(!checkDiagonal(mat, i, 0))
            return false;

    return true;
}

// Driver code
let mat = [
    [6, 7, 8],
    [4, 6, 7],
    [1, 4, 6]
];
if(isToeplitz(mat)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

[Expected Approach - 2] - Checking Diagonally Above Element - O(n × n) Time and O(1) Space

The core idea is to examine each cell, starting from the second row and second column. We compare each cell's value with its top-left neighbor. If a mismatch is found, the matrix is not Toeplitz, and we can stop. If the entire matrix is checked without any mismatches, all diagonals are constant.

Follow the below given steps:

  • Let n = mat.size() and m = mat[0].size().
  • Iterate i from 1 to n - 1 and within that j from 1 to m - 1.
  • If mat[i][j] != mat[i - 1][j - 1] at any point, return false.
  • Once all pairs have been checked with no mismatches, return true.
C++
#include <iostream>
#include<vector>
using namespace std;

// Function to check whether given
// matrix is toeplitz matrix or not
bool isToeplitz(vector<vector<int>> &mat) {
    int n = mat.size(), m = mat[0].size();

    // check diagonally above element of
    // each element in the matrix
    for(int i = 1; i < n; i++) {
        for(int j = 1; j < m; j++) {
            if(mat[i][j] != mat[i - 1][j - 1])
                return false;
        }
    }
    
    return true;
}

int main() {
    vector<vector<int>> mat = {
        {6, 7, 8},
        {4, 6, 7},
        {1, 4, 6}
    };
    if(isToeplitz(mat)) {
        cout << "true";
    } else {
        cout << "false";
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // Function to check whether given
    // matrix is toeplitz matrix or not
    static boolean isToeplitz(int[][] mat) {
        int n = mat.length, m = mat[0].length;

        // check diagonally above element of
        // each element in the matrix
        for(int i = 1; i < n; i++) {
            for(int j = 1; j < m; j++) {
                if(mat[i][j] != mat[i - 1][j - 1])
                    return false;
            }
        }
        
        return true;
    }

    public static void main(String[] args) {
        int[][] mat = {
            {6, 7, 8},
            {4, 6, 7},
            {1, 4, 6}
        };

        if(isToeplitz(mat)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
Python
# Function to check whether given
# matrix is toeplitz matrix or not
def isToeplitz(mat):
    n, m = len(mat), len(mat[0])

    # check diagonally above element of
    # each element in the matrix
    for i in range(1, n):
        for j in range(1, m):
            if mat[i][j] != mat[i - 1][j - 1]:
                return False

    return True

if __name__ == "__main__":
    mat = [
        [6, 7, 8],
        [4, 6, 7],
        [1, 4, 6]
    ]
if isToeplitz(mat):
    print("true")
else:
    print("false")
C#
using System;

class GfG {

    // Function to check whether given
    // matrix is toeplitz matrix or not
    static bool isToeplitz(int[,] mat) {
        int n = mat.GetLength(0), m = mat.GetLength(1);

        // check diagonally above element of
        // each element in the matrix
        for(int i = 1; i < n; i++) {
            for(int j = 1; j < m; j++) {
                if(mat[i, j] != mat[i - 1, j - 1])
                    return false;
            }
        }
        
        return true;
    }

    static void Main() {
        int[,] mat = {
            {6, 7, 8},
            {4, 6, 7},
            {1, 4, 6}
        };

        if(isToeplitz(mat)) {
            Console.WriteLine("true");
        } else {
            Console.WriteLine("false");
        }
    }
}
JavaScript
// Function to check whether given
// matrix is toeplitz matrix or not
function isToeplitz(mat) {
    let n = mat.length, m = mat[0].length;

    // check diagonally above element of
    // each element in the matrix
    for(let i = 1; i < n; i++) {
        for(let j = 1; j < m; j++) {
            if(mat[i][j] !== mat[i - 1][j - 1])
                return false;
        }
    }
    
    return true;
}


// Driver code
let mat = [
    [6, 7, 8],
    [4, 6, 7],
    [1, 4, 6]
];
if(isToeplitz(mat)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true
Comment