Largest Equal Squares with No Shared Cells

Quick Overview

Find the maximum area of each of two equally sized all-ones square submatrices that share no cells, allowing their boundaries to touch.

Largest Equal Squares with No Shared Cells

Company: American Express

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Largest Equal Squares with No Shared Cells You are given an `m`-by-`n` binary matrix `mat`. A cell is usable when it contains `1` and unusable when it contains `0`. Choose two square submatrices with the same positive integer side length `k`. Every cell of both squares must be usable, and the squares must not share any cell. Return the largest possible area of each square, `k * k`. If no pair exists, return `0`. ### Function `int maxArea(vector<vector<int>>& mat)` ### Example 1 **Input:** `mat = [[1, 1, 0, 1, 1], [1, 1, 0, 1, 1]]` **Output:** `4` **Explanation:** Choose the square with top-left corner `(0, 0)`, covering rows `0..1` and columns `0..1`, and the square with top-left corner `(0, 3)`, covering rows `0..1` and columns `3..4`. Both are all-ones squares of side `2`, and their cell sets are disjoint. A larger side length cannot fit in the two-row matrix. ### Example 2 **Input:** `mat = [[1, 1], [1, 1]]` **Output:** `1` **Explanation:** There is only one side-`2` square, so two such squares cannot be chosen. Two different usable cells form two disjoint side-`1` squares, each of area `1`. ### Constraints - `mat.length == m`, and each row has length `n`. - `1 <= m, n <= 500` - Every matrix entry is either `0` or `1`. ### Clarifications - Squares are aligned with the matrix rows and columns. A square starting at `(r, c)` covers rows `r..r + k - 1` and columns `c..c + k - 1`, inclusively. - Touching along a boundary or at a corner is allowed if no matrix cell belongs to both squares. - The two squares must have equal side length; return one square's area, not their combined area. - You must choose two squares. One usable cell alone is insufficient. ### What a Strong Answer Covers - Verifying that every covered cell is usable. - Enforcing equal side lengths and cell-disjointness, including boundary-touching cases. - Maximizing the per-square area and handling matrices with fewer than two usable cells.

Overview: Find the maximum area of each of two equally sized all-ones square submatrices that share no cells, allowing their boundaries to touch.

American Express logo
American Express
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Largest Equal Squares with No Shared Cells

You are given an m-by-n binary matrix mat. A cell is usable when it contains 1 and unusable when it contains 0.

Choose two square submatrices with the same positive integer side length k. Every cell of both squares must be usable, and the squares must not share any cell.

Return the largest possible area of each square, k * k. If no pair exists, return 0.

Function

int maxArea(vector<vector<int>>& mat)

Example 1

Input: mat = [[1, 1, 0, 1, 1], [1, 1, 0, 1, 1]]

Output: 4

Explanation: Choose the square with top-left corner (0, 0), covering rows 0..1 and columns 0..1, and the square with top-left corner (0, 3), covering rows 0..1 and columns 3..4. Both are all-ones squares of side 2, and their cell sets are disjoint. A larger side length cannot fit in the two-row matrix.

Example 2

Input: mat = [[1, 1], [1, 1]]

Output: 1

Explanation: There is only one side-2 square, so two such squares cannot be chosen. Two different usable cells form two disjoint side-1 squares, each of area 1.

Constraints

  • mat.length == m , and each row has length n .
  • 1 <= m, n <= 500
  • Every matrix entry is either 0 or 1 .

Clarifications

  • Squares are aligned with the matrix rows and columns. A square starting at (r, c) covers rows r..r + k - 1 and columns c..c + k - 1 , inclusively.
  • Touching along a boundary or at a corner is allowed if no matrix cell belongs to both squares.
  • The two squares must have equal side length; return one square's area, not their combined area.
  • You must choose two squares. One usable cell alone is insufficient.

What a Strong Answer Covers Guidance

  • Verifying that every covered cell is usable.
  • Enforcing equal side lengths and cell-disjointness, including boundary-touching cases.
  • Maximizing the per-square area and handling matrices with fewer than two usable cells.