VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-rectangle-binary-matrix-corners-1/

⇱ Submatrix with corners as 1 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Submatrix with corners as 1

Last Updated : 16 Feb, 2026

Given a binary matrix containing only 0s and 1s. The task is to check whether there exists a rectangle or square submatrix of size at least 2 Ɨ 2 such that all four corners of that submatrix are 1.
This means we must find four positions in the matrix (top-left, top-right, bottom-left, and bottom-right, that are all 1, and they must form the corners of a valid rectangle).

Examples:

Input: mat[][] = [[1, 0, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[1, 0, 1, 0, 1]]
Output : true
Explanation : There exists the below submatrix
1 0 1
0 1 0
1 0 1

Input: mat[][] = [[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 1, 1]]
Output : false
Explanation : There exists the below submatrix:
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1

Input: mat[][] = [[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 1]]
Output : false
Explanation: There is no rectangle with all corners as 1.

[Naive Approach] Check Every Submatrix - O((n^2)*(m^2)) Time and O(1) Space

The idea is to check all possible submatrices of size at least 2 Ɨ 2 by iterating through every pair of rows and every pair of columns. For each such combination (quadruple), we verify if the four corners of the submatrix are all 1s. If such a rectangle is found, we return true immediately. This approach ensures that we don't miss any valid configuration.


Output
true

[Better Approach] Using Hashing - O(n*(m^2)) Time and O(n*m) Space

The idea is to use hashing to avoid checking every submatrix explicitly. We need to track column pairs with 1s in each row using a set. If the same pair of columns has 1s in more than one row, then a submatrix with all 1s at corners must exist. This works because repeating column pairs across rows form the top and bottom of a valid submatrix.


Output
true

[Expected Approach] Using Sparse-Dense Row Strategy

Instead of checking every possible rectangle directly, it optimizes the search by analyzing rows with 1s and checking for repeating column pairs. Rows are categorized as dense or sparse to reduce time complexity by using different strategies for each.

We can see that two rows share at least two common columns with 1s, then those column indices form the vertical sides of a rectangle, and the two rows form the horizontal sides. So, we only need to find such row pairs with common 1-columns at the same indices.

So, to Solve this, First processes each row to record the columns that have 1s. For sparse rows (having fewer 1s), it stores every unique pair of column indices and checks if the same pair appeared before — indicating a rectangle with a previous row. For dense rows (many 1s), it compares against all earlier rows to count how many 1-columns they share, and returns true if at least two are common.


Output
true

Time complexity: O(r * c + N√N + √N * c²), where N is the total number of 1s in the matrix and r and c are the number of rows and columns respectively. In the worst case (N ā‰ˆ r * c), this becomes O(r * c * √(r * c))
Auxiliary space:O(N + c²), for storing column indices of all 1’s in the matrix and for maintaining the column-pair tracking structure.

Comment