Watch 10 video solutions for Maximize Area of Square Hole in Grid, a medium level problem involving Array, Sorting. This walkthrough by codestorywithMIK has 9,130 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given the two integers, n and m and two integer arrays, hBars and vBars. The grid has n + 2 horizontal and m + 2 vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from 1.
You can remove some of the bars in hBars from horizontal bars and some of the bars in vBars from vertical bars. Note that other bars are fixed and cannot be removed.
Return an integer denoting the maximum area of a square-shaped hole in the grid, after removing some bars (possibly none).
Example 1:

Input: n = 2, m = 1, hBars = [2,3], vBars = [2]
Output: 4
Explanation:
The left image shows the initial grid formed by the bars. The horizontal bars are [1,2,3,4], and the vertical bars are [1,2,3].
One way to get the maximum square-shaped hole is by removing horizontal bar 2 and vertical bar 2.
Example 2:

Input: n = 1, m = 1, hBars = [2], vBars = [2]
Output: 4
Explanation:
To get the maximum square-shaped hole, we remove horizontal bar 2 and vertical bar 2.
Example 3:

Input: n = 2, m = 3, hBars = [2,3], vBars = [2,4]
Output: 4
Explanation:
One way to get the maximum square-shaped hole is by removing horizontal bar 3, and vertical bar 4.
Constraints:
1 <= n <= 1091 <= m <= 1091 <= hBars.length <= 1002 <= hBars[i] <= n + 11 <= vBars.length <= 1002 <= vBars[i] <= m + 1hBars are distinct.vBars are distinct.Problem Overview: You are given horizontal and vertical bars removed from a grid. Removing consecutive bars creates a larger hole. The goal is to compute the maximum possible square area that can be formed from these removed bars.
Approach 1: Recursive Depth-First Search (DFS) Grid Exploration (Time: O(n*m), Space: O(n*m))
Model the grid as a matrix where removed bars create open cells. Starting from each open position, run a recursive DFS to explore connected regions. Track the largest square that can be formed inside that region by expanding while neighbors remain open. DFS repeatedly visits neighbors using recursion and marks cells as visited to avoid revisiting. This approach works conceptually but simulating the grid is expensive when the grid dimensions are large.
Approach 2: Iterative Breadth-First Search (BFS) Region Expansion (Time: O(n*m), Space: O(n*m))
Instead of recursion, use a queue-based BFS to explore open regions formed by removed bars. Push the starting cell into a queue and iteratively expand to neighbors. BFS processes cells layer by layer and records the width and height of reachable empty space. From this region size you compute the largest possible square. This approach avoids recursion depth limits and provides predictable memory usage, but it still requires explicitly constructing and traversing the grid.
Approach 3: Sorting Consecutive Bars (Optimal) (Time: O(k log k), Space: O(1))
The key observation: removing k consecutive horizontal bars creates a vertical gap of size k + 1. The same applies to vertical bars. Instead of exploring the grid, sort the removed bar indices and scan them to find the longest consecutive sequence. For example, if horizontal bars [2,3,4] are removed, the gap height becomes 4. Do this independently for horizontal and vertical arrays. The largest square side equals min(maxHorizontalGap, maxVerticalGap). The square area is that side squared.
This approach relies on simple array operations: sort, iterate once to count consecutive sequences, and track the maximum run length. No grid simulation is required. Because the logic operates only on the removed bar arrays, it scales well even when the grid size itself is very large.
Related concepts frequently appear in problems involving arrays and sorting, where ordering elements reveals structural patterns such as consecutive runs or gaps.
Recommended for interviews: The sorting-based approach is what interviewers typically expect. It shows you recognized the mathematical structure of consecutive bars rather than simulating the entire grid. Explaining the DFS or BFS idea first demonstrates problem exploration, but moving to the sort + consecutive scan solution shows strong optimization instincts.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive DFS Grid Exploration | O(n*m) | O(n*m) | Conceptual approach when modeling the grid explicitly |
| Iterative BFS Region Expansion | O(n*m) | O(n*m) | When avoiding recursion depth or exploring regions level by level |
| Sorting Consecutive Bars (Optimal) | O(k log k) | O(1) | Best approach when only removed bar indices matter |