algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
2 shown · 0/261 solved

Largest Pair of Squares

GoldCommunity Beta
Asked atAmerican Express
Solve problem →
3000ms256MBAdded Aug 20, 2026Grid DP

You are given an n x m grid of 0s and 1s.

Place two axis-aligned squares on the grid such that:

  • both squares have the same side length,
  • every cell inside each square is 1,
  • the two squares do not share any cell.

A single cell (1 x 1) counts as a valid square.

Return the maximum possible side length of such a pair of squares, or 0 if no valid pair exists.

Examples

Example 1

Input: grid = [[1,1,0],
               [1,1,1],
               [0,1,1]]
Output: 1
Explanation: There are 2 x 2 all-1 squares (top-left and bottom-right), but any
two of them share the center cell. Two disjoint 1 x 1 squares exist, so the
answer is 1.

Example 2

Input: grid = [[1,1,1,1],
               [1,1,1,1]]
Output: 2
Explanation: The left 2 x 2 square (columns 0-1) and the right 2 x 2 square
(columns 2-3) are both all-1 and disjoint.

Constraints

  • 1≤n,m≤7001 \leq n, m \leq 7001≤n,m≤700
  • grid[i][j] is 0 or 1

Details

Solved by3 people
Time limit3000ms
Memory256MB
AddedAug 20, 20263 weeks ago
Grid DP

More like this