Maximize Area of Square Hole in Grid - Problem
Maximize Area of Square Hole in Grid

Imagine you have a grid made up of removable bars - some horizontal, some vertical. Your mission is to strategically remove some of these bars to create the largest possible square-shaped hole in the grid!

You're given:
• A grid with n + 2 horizontal bars and m + 2 vertical bars
• Two arrays: hBars (removable horizontal bars) and vBars (removable vertical bars)
• All other bars are fixed and cannot be removed

The bars create a grid of 1×1 unit cells. When you remove consecutive bars, you create holes. Your goal is to find the maximum area of a square-shaped hole you can create by removing some (or none) of the bars from the given arrays.

Key insight: To form a square hole of side length k, you need k consecutive removable bars both horizontally and vertically!

Input & Output

example_1.py — Basic Case
$ Input: n = 2, m = 1, hBars = [2,3], vBars = [2]
Output: 4
💡 Note: We can remove horizontal bars 2 and 3 (consecutive) and vertical bar 2. This creates a 2×2 square hole with area 4.
example_2.py — Larger Grid
$ Input: n = 1, m = 3, hBars = [2], vBars = [2,3,4]
Output: 4
💡 Note: We can remove horizontal bar 2 and vertical bars 2,3 (consecutive). The square is limited by the shorter dimension: min(1,2)+1 = 2, so area = 4.
example_3.py — No Consecutive Bars
$ Input: n = 2, m = 3, hBars = [2,4], vBars = [2,4,6]
Output: 1
💡 Note: No consecutive bars can be removed, so we can only create individual 1×1 holes. Maximum area is 1.

Visualization

Tap to expand
Grid Hole FormationStep 1: Original GridRemovable: H[2,3], V[2,3]Step 2: Remove Consecutive Bars2×2Removed: H[2,3], V[2,3]Algorithm Steps1. Sort removable bars: H=[2,3], V=[2,3]2. Find consecutive sequences: H_max=2, V_max=23. Calculate square: min(2,2)+1 = 3×3 = 94. Return maximum area = 9Time Complexity: O((h+v) log(h+v)) | Space Complexity: O(1)
Understanding the Visualization
1
Identify Removable Bars
Mark which horizontal and vertical bars can be removed from the grid
2
Sort by Position
Arrange removable bars in order to easily find consecutive sequences
3
Find Consecutive Groups
Locate the longest consecutive sequences in both directions
4
Calculate Square Size
The square is constrained by the shorter of the two consecutive sequences
Key Takeaway
🎯 Key Insight: The maximum square hole is constrained by the shorter of the two longest consecutive removable bar sequences, requiring only sorting and linear scanning for an optimal solution.

Time & Space Complexity

Time Complexity
⏱️
O((h + v) log(h + v))

Sorting both arrays takes O(h log h + v log v), finding consecutive sequences takes O(h + v)

n
2n
Linearithmic
Space Complexity
O(1)

Only using a few variables to track consecutive sequences, sorting can be done in-place

n
2n
Linear Space

Constraints

  • 1 ≤ n, m ≤ 105
  • 1 ≤ hBars.length, vBars.length ≤ 105
  • 1 ≤ hBars[i] ≤ n + 1
  • 1 ≤ vBars[i] ≤ m + 1
  • All values in hBars and vBars are unique
  • Bars are indexed from 1 to n+1 (horizontal) and 1 to m+1 (vertical)
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
23.4K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen