Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

766. Toeplitz Matrix

Easy69.3% Acceptance
ArrayMatrix
Asked by:
F
Facebook
Bloomberg
ProblemHints (1)Solutions (8)VideosCompanies (2)Notes

Problem Statement

Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.

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

Example 1:

Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
Explanation:
In the above grid, the diagonals are:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements are the same, so the answer is True.

Example 2:

Input: matrix = [[1,2],[2,2]]
Output: false
Explanation:
The diagonal "[1, 2]" has different elements.

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 20
  • 0 <= matrix[i][j] <= 99

Follow up:

  • What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
  • What if the matrix is so large that you can only load up a partial row into the memory at once?
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
B

Approach

In #766 Toeplitz Matrix, the goal is to determine whether every diagonal from the top-left to the bottom-right contains the same value. A matrix is considered Toeplitz if each element matches the element diagonally up-left from it.

The most straightforward idea is to iterate through the matrix and compare each element matrix[i][j] with its top-left neighbor matrix[i-1][j-1]. If any pair differs, the matrix is not Toeplitz. Since the first row and first column do not have top-left neighbors, comparisons start from index (1,1). This approach efficiently validates all diagonals in a single pass.

An alternative conceptual approach is to track diagonals using a map keyed by i - j, since elements on the same diagonal share the same difference. Both strategies work well, but the direct comparison approach is simpler and uses constant extra space.

The traversal visits each cell once, leading to O(m × n) time complexity and O(1) additional space for the optimal solution.

Complexity

ApproachTime ComplexitySpace Complexity
Neighbor Diagonal ComparisonO(m × n)O(1)
Hash Map by Diagonal (i - j)O(m × n)O(min(m, n))

Video Solution Available

Programmer Mitch

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Check whether each value is equal to the value of it's top-left neighbor.

Ready to see the solutions?View Solutions

Solutions (8)

Approach 1: Iterative Check for Diagonal Consistency

This method involves checking each element of the matrix to ensure that it equals the element diagonally ahead of it - that is, for each cell matrix[i][j], it should be equal to matrix[i+1][j+1], provided both indices are within bounds.

Time Complexity: O(m * n) where m is the number of rows and n is the number of columns.
Space Complexity: O(1), since no additional data structures are used.

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2
3bool isToeplitzMatrix(int** matrix, int matrixRowSize, int *matrixColSizes) {
4    for (int i = 0

Explanation

In C, we use nested loops to iterate over each element, excluding the last row and column, to check if matrix[i][j] equals matrix[i+1][j+1]. If any element fails this check, we immediately return false.

Approach 2: Use HashMap to Track Diagonals

We can use a HashMap (or dictionary) to maintain the first element of each diagonal. Each key represents the difference between row and column indices, and the value is the first element at this diagonal. While iterating, if a new element violates this rule, the matrix isn't Toeplitz.

Time Complexity: O(m * n)
Space Complexity: O(m + n) for storing diagonal mappings.

PythonJava
1def isToeplitzMatrix(matrix):
2    diagonal_map = {

Video Solutions

Watch expert explanations and walkthroughs

Toeplitz Matrix (Leetcode #766)

Programmer Mitch
9:357,700 views

Asked By Companies

2 companies
F
Facebook
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Valid SudokuMedium
Sudoku SolverHard
Rotate ImageMedium
Spiral MatrixMedium
More similar problems

Related Topics

ArrayMatrix

Problem Stats

Acceptance Rate69.3%
DifficultyEasy
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Toeplitz Matrix asked in FAANG interviews?

Yes, variations of matrix validation problems like Toeplitz Matrix are commonly asked in technical interviews. They test understanding of matrix traversal, indexing patterns, and edge-case handling.

What is the optimal approach for Toeplitz Matrix?

The optimal approach checks whether each element matches its top-left diagonal neighbor. By iterating from the second row and second column and comparing matrix[i][j] with matrix[i-1][j-1], we can validate the Toeplitz property in a single pass with constant extra space.

What data structure is best for solving Toeplitz Matrix?

A simple 2D array traversal is sufficient for the optimal solution. Alternatively, a hash map keyed by the diagonal index (i - j) can be used to group elements belonging to the same diagonal.

Why do elements on the same diagonal share the same i - j value?

For any element in a matrix, moving one step down and one step right increases both row and column indices equally. This keeps the difference (i - j) constant, which uniquely identifies a top-left to bottom-right diagonal.

;
i
<
matrixRowSize
-
1
;
i
++
)
{
5
for
(
int
j
=
0
;
j
<
matrixColSizes
[
i
]
-
1
;
j
++
)
{
6
if
(
matrix
[
i
]
[
j
]
!=
matrix
[
i
+
1
]
[
j
+
1
]
)
{
7
return
false
;
8
}
9
}
10
}
11
return
true
;
12
}
}
3
for
i
in
range
(
len
(
matrix
)
)
:
4
for
j
in
range
(
len
(
matrix
[
0
]
)
)
:
5
if
i
-
j
not
in
diagonal_map
:
6
diagonal_map
[
i
-
j
]
=
matrix
[
i
]
[
j
]
7
elif
diagonal_map
[
i
-
j
]
!=
matrix
[
i
]
[
j
]
:
8
return
False
9
return
True

Explanation

In this Python implementation, a dictionary keeps track of each diagonal. The key (i - j) refers to a specific diagonal, and its value is the element that should be the same for all elements in this diagonal.