Dsa
Matrix
Medium
Count Diagonal Lines of 1s
Given a 2D binary matrix, count the number of diagonal lines that contain '1's. A diagonal is defined as a sequence of adjacent '1's that share the same slope, either left-to-right or right-to-left.
Input Format:
- The first line contains two integers m (rows) and n (columns) of the matrix.
- The next m lines contain n characters (either '0' or '1').
Output Format:
- A single integer representing the number of diagonal lines of '1's.
Example:
Input:
4 5
1 0 0 1 0
0 1 1 0 0
0 0 1 0 1
1 0 0 1 1
Output:
6
Key concepts
matrixdiagonalscounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now