Given an n * m binary matrix mat[][] containing only 0s and 1s, determine if there exists a rectangle within the matrix such that all four corners of the rectangle are 1. If such a rectangle exists, return true; otherwise, return false.
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: Valid corners are at index (1,2), (1,4), (3,2), (3,4)Input: mat[][] = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Output: false
Explanation: There are no valid corners.
Table of Content
[Naive Approach] Check Every Rectangle - O(n ^ 2 * m ^ 2) Time and O(1) Space
The idea is to check every possible rectangle in the matrix by considering every pair of rows and every pair of columns. For each combination, we verify whether all four corner cells are 1. If such a rectangle is found, we immediately return true; otherwise, after checking all possible rectangles, we return false.
Working of Approach:
- Iterate through every possible pair of rows.
- For each pair of rows, iterate through every possible pair of columns.
- Treat the selected rows and columns as the four corners of a rectangle.
- Check whether all four corner cells contain 1.
- If yes, return true; otherwise continue checking all possibilities.
#include <bits/stdc++.h>
using namespace std;
bool validCorner(vector<vector<int>> &mat)
{
int rows = mat.size();
int cols = mat[0].size();
// Check every pair of rows
for (int top = 0; top < rows - 1; top++)
{
for (int bottom = top + 1; bottom < rows; bottom++)
{
// Check every pair of columns
for (int left = 0; left < cols - 1; left++)
{
for (int right = left + 1; right < cols; right++)
{
// Check whether all four corners are 1
if (mat[top][left] && mat[top][right] && mat[bottom][left] && mat[bottom][right])
{
return true;
}
}
}
}
}
return false;
}
int main()
{
vector<vector<int>> mat = {{1, 0, 0, 1, 0}, {0, 0, 1, 0, 1}, {0, 0, 0, 1, 0}, {1, 0, 1, 0, 1}};
if (validCorner(mat))
cout << "true";
else
cout << "false";
return 0;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GFG {
public static boolean validCorner(int[][] mat)
{
int rows = mat.length;
int cols = mat[0].length;
// Check every pair of rows
for (int top = 0; top < rows - 1; top++) {
for (int bottom = top + 1; bottom < rows;
bottom++) {
// Check every pair of columns
for (int left = 0; left < cols - 1;
left++) {
for (int right = left + 1; right < cols;
right++) {
// Check whether all four corners
// are 1
if (mat[top][left] == 1
&& mat[top][right] == 1
&& mat[bottom][left] == 1
&& mat[bottom][right] == 1) {
return true;
}
}
}
}
}
return false;
}
public static void main(String[] args)
{
int[][] mat = { { 1, 0, 0, 1, 0 },
{ 0, 0, 1, 0, 1 },
{ 0, 0, 0, 1, 0 },
{ 1, 0, 1, 0, 1 } };
if (validCorner(mat))
System.out.println("true");
else
System.out.println("false");
}
}
def validCorner(mat):
rows = len(mat)
cols = len(mat[0])
# Check every pair of rows
for top in range(rows - 1):
for bottom in range(top + 1, rows):
# Check every pair of columns
for left in range(cols - 1):
for right in range(left + 1, cols):
# Check whether all four corners are 1
if mat[top][left] and mat[top][right] and mat[bottom][left] and mat[bottom][right]:
return True
return False
if __name__ == '__main__':
mat = [[1, 0, 0, 1, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 0], [1, 0, 1, 0, 1]]
if validCorner(mat):
print('true')
else:
print('false')
using System;
public class GFG {
public static bool validCorner(int[][] mat)
{
int rows = mat.Length;
int cols = mat[0].Length;
// Check every pair of rows
for (int top = 0; top < rows - 1; top++) {
for (int bottom = top + 1; bottom < rows;
bottom++) {
// Check every pair of columns
for (int left = 0; left < cols - 1;
left++) {
for (int right = left + 1; right < cols;
right++) {
// Check whether all four corners
// are 1
if (mat[top][left] == 1
&& mat[top][right] == 1
&& mat[bottom][left] == 1
&& mat[bottom][right] == 1) {
return true;
}
}
}
}
}
return false;
}
public static void Main()
{
int[][] mat
= new int[][] { new int[] { 1, 0, 0, 1, 0 },
new int[] { 0, 0, 1, 0, 1 },
new int[] { 0, 0, 0, 1, 0 },
new int[] { 1, 0, 1, 0, 1 } };
if (validCorner(mat))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function validCorner(mat)
{
let rows = mat.length;
let cols = mat[0].length;
// Check every pair of rows
for (let top = 0; top < rows - 1; top++) {
for (let bottom = top + 1; bottom < rows;
bottom++) {
// Check every pair of columns
for (let left = 0; left < cols - 1; left++) {
for (let right = left + 1; right < cols;
right++) {
// Check whether all four corners are 1
if (mat[top][left] && mat[top][right]
&& mat[bottom][left]
&& mat[bottom][right]) {
return true;
}
}
}
}
}
return false;
}
// Driver Code
let mat = [
[ 1, 0, 0, 1, 0 ], [ 0, 0, 1, 0, 1 ], [ 0, 0, 0, 1, 0 ],
[ 1, 0, 1, 0, 1 ]
];
if (validCorner(mat))
console.log("true");
else
console.log("false");
Output
true
[Expected Approach] Compare Every Pair of Rows - O(n ^ 2 * m) Time and O(1) Space
The idea is to consider every pair of rows and count the columns where both rows contain 1. If two rows share at least two common columns containing 1, then those two rows and two columns form a rectangle whose four corners are 1. Thus, we can immediately return true without checking every possible column pair explicitly.
Working of Approach:
- Iterate through every pair of rows.
- For the current pair, traverse all columns.
- Count the columns where both rows have a 1.
- If the count becomes at least 2, a rectangle with four corners as 1 exists.
- Otherwise, continue checking the remaining row pairs.
Let us understand with an example:
Input: mat[][] = [[1, 0, 0, 1, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 0], [1, 0, 1, 0, 1]]
- Compare rows 0 and 1; they do not share two columns containing 1, so no rectangle is formed.
- Compare rows 0 and 3; only column 0 contains 1 in both rows, so continue searching.
- Compare rows 1 and 3; both rows contain 1 at columns 2 and 4, giving two common columns.
- These two rows and two columns form a rectangle with corners (1,2), (1,4), (3,2), and (3,4).
- Since a valid rectangle is found, the function returns true.
#include <bits/stdc++.h>
using namespace std;
bool validCorner(vector<vector<int>> &mat)
{
int rows = mat.size();
if (rows == 0)
return false;
int columns = mat[0].size();
// Iterate over all pairs of rows
for (int i = 0; i < rows; i++)
{
for (int p = i + 1; p < rows; p++)
{
int count = 0;
// Count columns where both rows have 1
for (int k = 0; k < columns; k++)
{
if (mat[i][k] == 1 && mat[p][k] == 1)
{
count++;
}
}
// If there are at least two such columns, a rectangle exists
if (count >= 2)
{
return true;
}
}
}
return false;
}
int main()
{
vector<vector<int>> mat = {{1, 0, 0, 1, 0}, {0, 0, 1, 0, 1}, {0, 0, 0, 1, 0}, {1, 0, 1, 0, 1}};
if (validCorner(mat))
cout << "true";
else
cout << "false";
return 0;
}
import java.util.Arrays;
public class GFG {
public static boolean validCorner(int[][] mat)
{
int rows = mat.length;
if (rows == 0)
return false;
int columns = mat[0].length;
// Iterate over all pairs of rows
for (int i = 0; i < rows; i++) {
for (int p = i + 1; p < rows; p++) {
int count = 0;
// Count columns where both rows have 1
for (int k = 0; k < columns; k++) {
if (mat[i][k] == 1 && mat[p][k] == 1) {
count++;
}
}
// If there are at least two such columns, a
// rectangle exists
if (count >= 2) {
return true;
}
}
}
return false;
}
public static void main(String[] args)
{
int[][] mat = { { 1, 0, 0, 1, 0 },
{ 0, 0, 1, 0, 1 },
{ 0, 0, 0, 1, 0 },
{ 1, 0, 1, 0, 1 } };
if (validCorner(mat))
System.out.println("true");
else
System.out.println("false");
}
}
def validCorner(mat):
rows = len(mat)
if rows == 0:
return False
columns = len(mat[0])
# Iterate over all pairs of rows
for i in range(rows):
for p in range(i + 1, rows):
count = 0
# Count columns where both rows have 1
for k in range(columns):
if mat[i][k] == 1 and mat[p][k] == 1:
count += 1
# If there are at least two such columns, a rectangle exists
if count >= 2:
return True
return False
if __name__ == '__main__':
mat = [[1, 0, 0, 1, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 0], [1, 0, 1, 0, 1]]
if validCorner(mat):
print('true')
else:
print('false')
using System;
public class GFG {
public static bool validCorner(int[][] mat)
{
int rows = mat.Length;
if (rows == 0)
return false;
int columns = mat[0].Length;
// Iterate over all pairs of rows
for (int i = 0; i < rows; i++) {
for (int p = i + 1; p < rows; p++) {
int count = 0;
// Count columns where both rows have 1
for (int k = 0; k < columns; k++) {
if (mat[i][k] == 1 && mat[p][k] == 1) {
count++;
}
}
// If there are at least two such columns, a
// rectangle exists
if (count >= 2) {
return true;
}
}
}
return false;
}
public static void Main()
{
int[][] mat
= new int[][] { new int[] { 1, 0, 0, 1, 0 },
new int[] { 0, 0, 1, 0, 1 },
new int[] { 0, 0, 0, 1, 0 },
new int[] { 1, 0, 1, 0, 1 } };
if (validCorner(mat))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function validCorner(mat)
{
let rows = mat.length;
if (rows === 0)
return false;
let columns = mat[0].length;
// Iterate over all pairs of rows
for (let i = 0; i < rows; i++) {
for (let p = i + 1; p < rows; p++) {
let count = 0;
// Count columns where both rows have 1
for (let k = 0; k < columns; k++) {
if (mat[i][k] === 1 && mat[p][k] === 1) {
count++;
}
}
// If there are at least two such columns, a
// rectangle exists
if (count >= 2) {
return true;
}
}
}
return false;
}
// Driver Code
let mat = [
[ 1, 0, 0, 1, 0 ], [ 0, 0, 1, 0, 1 ], [ 0, 0, 0, 1, 0 ],
[ 1, 0, 1, 0, 1 ]
];
if (validCorner(mat))
console.log("true");
else
console.log("false");
Output
true