Maximum Square Area by Removing Fences From a Field - Problem

Imagine you're a landscape architect working with a large rectangular field that's divided by various fences. Your goal is to create the largest possible square area by strategically removing some of these fences!

You have a field with dimensions (m-1) × (n-1) that spans from coordinate (1,1) to (m,n). The field contains:

  • Horizontal fences: Each fence at position hFences[i] runs from (hFences[i], 1) to (hFences[i], n)
  • Vertical fences: Each fence at position vFences[i] runs from (1, vFences[i]) to (m, vFences[i])

Important: The field has a permanent border (four outer fences) that cannot be removed.

Your task is to find the maximum area of a square that can be formed by removing some fences (or keeping all of them). Return the area modulo 10^9 + 7, or -1 if no square can be formed.

Input & Output

example_1.py — Basic Square Formation
$ Input: m = 4, n = 3, hFences = [2, 3], vFences = [2]
Output: 4
💡 Note: We can form a square with side length 2 by using boundaries at positions 1 and 3 horizontally, and 1 and 3 vertically. This gives us area = 2² = 4.
example_2.py — Large Square Possible
$ Input: m = 6, n = 7, hFences = [2], vFences = [4]
Output: 9
💡 Note: The maximum square has side length 3, formed using various fence combinations. The area is 3² = 9.
example_3.py — No Square Possible
$ Input: m = 4, n = 3, hFences = [], vFences = []
Output: 9
💡 Note: With just the boundary fences, we can form a 3×3 square (limited by the smaller dimension), so area = 9.

Visualization

Tap to expand
Field Boundary (Permanent)Valid Square🔍 Strategy:1. Identify all possible distances2. Find common H & V distances3. Choose maximum for largest areaOptimization Key:Use hash sets for O(1) lookups!
Understanding the Visualization
1
Map the Field
Identify all fence positions including permanent boundaries
2
Calculate Distances
Compute all possible horizontal and vertical distances
3
Find Valid Squares
Identify distances that work for both dimensions
4
Maximize Area
Select the largest valid square and return its area
Key Takeaway
🎯 Key Insight: By pre-computing all possible distances and using hash sets for intersection, we can efficiently find the maximum valid square without checking every combination individually.

Time & Space Complexity

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

O(h²) to generate horizontal distances and O(v²) for vertical distances, where h and v are number of fences

n
2n
Linear Growth
Space Complexity
O(h² + v²)

Space needed to store all possible horizontal and vertical distances in hash sets

n
2n
Linear Space

Constraints

  • 3 ≤ m, n ≤ 109
  • 1 ≤ hFences.length, vFences.length ≤ 1000
  • 1 < hFences[i] < m
  • 1 < vFences[i] < n
  • All fence positions are unique within their respective arrays
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
23.5K Views
Medium-High Frequency
~25 min Avg. Time
847 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