Skip to content

Practice Maximum Square Area by Removing Fences From a Field

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2975
LeetCode Problem

Maximum Square Area by Removing Fences From a Field

Problem Statement

The field extends from (1,1) to (m,n). hFences gives horizontal fence coordinates and vFences vertical ones. Remove any interior fences to create the largest square region; the four outer boundary fences cannot be removed. Return its area modulo 10^9 + 7, or -1 if no square can be formed.

Source: Maximum Square Area by Removing Fences From a Field on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
m = 4, n = 3, hFences = [2,3], vFences = [2]
Output
4

# Example 2

Input
m = 6, n = 7, hFences = [2], vFences = [4]
Output
-1

Constraints

3 <= m, n <= 10^9
1 <= hFences.length, vFences.length <= 600
1 < hFences[i] < m
1 < vFences[i] < n
hFences and vFences are unique.

Related LeetCode Problems

Practice more problems similar to Maximum Square Area by Removing Fences From a Field.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.