There is a large (m - 1) x (n - 1) rectangular field with corners at (1, 1) and (m, n) containing some horizontal and vertical fences given in arrays hFences and vFences respectively.
Horizontal fences are from the coordinates (hFences[i], 1) to (hFences[i], n) and vertical fences are from the coordinates (1, vFences[i]) to (m, vFences[i]).
Return _themaximum area of a square field that can be formed by
removing some fences (possibly none) or _-1if it is impossible to make a square field.
Since the answer may be large, return it modulo10^9 + 7.
Note: The field is surrounded by two horizontal fences from the coordinates (1, 1) to (1, n) and (m, 1) to (m, n) and two vertical fences from the coordinates (1, 1) to (m, 1) and (1, n) to (m, n).
These fences cannot be removed.
Input: m =4, n =3, hFences =[2,3], vFences =[2]Output: 4Explanation: Removing the horizontal fence at 2 and the vertical fence at 2 will give a square field of area 4.

Input: m =6, n =7, hFences =[2], vFences =[4]Output: -1Explanation: It can be proved that there is no way to create a square field by removing fences.
The largest square area is determined by the largest distance that appears as both a horizontal and vertical gap between fences. By removing fences, we can create larger gaps. We want the largest distance that is present in both the set of possible horizontal and vertical gaps.
classSolution {
public:int maxSquareArea(int m, int n, vector<int>& hFences, vector<int>& vFences) {
hFences.push_back(1); hFences.push_back(m);
vFences.push_back(1); vFences.push_back(n);
sort(hFences.begin(), hFences.end());
sort(vFences.begin(), vFences.end());
unordered_set<int> hSet, vSet;
for (int i =0; i < hFences.size(); ++i)
for (int j = i+1; j < hFences.size(); ++j)
hSet.insert(hFences[j] - hFences[i]);
for (int i =0; i < vFences.size(); ++i)
for (int j = i+1; j < vFences.size(); ++j)
vSet.insert(vFences[j] - vFences[i]);
int ans =-1;
for (int d : hSet) if (vSet.count(d)) ans = max(ans, d);
if (ans ==-1) return-1;
return (1LL* ans * ans) %1000000007;
}
};
classSolution {
publicintmaxSquareArea(int m, int n, int[] hFences, int[] vFences) {
List<Integer> h =new ArrayList<>();
List<Integer> v =new ArrayList<>();
for (int x : hFences) h.add(x);
for (int x : vFences) v.add(x);
h.add(1); h.add(m);
v.add(1); v.add(n);
Collections.sort(h); Collections.sort(v);
Set<Integer> hSet =new HashSet<>(), vSet =new HashSet<>();
for (int i = 0; i < h.size(); i++)
for (int j = i+1; j < h.size(); j++)
hSet.add(h.get(j) - h.get(i));
for (int i = 0; i < v.size(); i++)
for (int j = i+1; j < v.size(); j++)
vSet.add(v.get(j) - v.get(i));
int ans =-1;
for (int d : hSet) if (vSet.contains(d)) ans = Math.max(ans, d);
if (ans ==-1) return-1;
return (int)(((long)ans * ans) % 1000000007);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funmaxSquareArea(m: Int, n: Int, hFences: IntArray, vFences: IntArray): Int {
val h = hFences.toMutableList().apply { add(1); add(m) }.sorted()
val v = vFences.toMutableList().apply { add(1); add(n) }.sorted()
val hSet = mutableSetOf<Int>()
val vSet = mutableSetOf<Int>()
for (i in h.indices) for (j in i+1 until h.size) hSet.add(h[j] - h[i])
for (i in v.indices) for (j in i+1 until v.size) vSet.add(v[j] - v[i])
val ans = hSet.filter { itin vSet }.maxOrNull() ?: -1returnif (ans == -1) -1else ((ans.toLong() * ans) % 1000000007L).toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defmaxSquareArea(self, m: int, n: int, hFences: list[int], vFences: list[int]) -> int:
hFences = sorted(hFences + [1, m])
vFences = sorted(vFences + [1, n])
hSet = set()
vSet = set()
for i in range(len(hFences)):
for j in range(i+1, len(hFences)):
hSet.add(hFences[j] - hFences[i])
for i in range(len(vFences)):
for j in range(i+1, len(vFences)):
vSet.add(vFences[j] - vFences[i])
ans = max((d for d in hSet if d in vSet), default=-1)
return-1if ans ==-1else (ans * ans) %1_000_000_007