There are n points on an infinite plane. You are given two integer arrays
xCoord and yCoord where (xCoord[i], yCoord[i]) represents the coordinates of the ith point.
Your task is to find the maximum area of a rectangle that:
Can be formed using four of these points as its corners.
Does not contain any other point inside or on its border.
Has its edges parallel to the axes.
Return the maximum area that you can obtain or -1 if no such rectangle is possible.
Input:ord =[1,1,3,3], yCoord =[1,3,1,3]Output: 4Explanation:
****
We can make a rectangle with these 4 points as corners and there is no other
point that lies inside or on the border. Hence, the maximum possible area
would be 4.
Input:ord =[1,1,3,3,2], yCoord =[1,3,1,3,2]Output: -1Explanation:
****
There is only one rectangle possible iswith points `[1,1], [1,3], [3,1]` and
`[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1.
Input:ord =[1,1,3,3,1,3], yCoord =[1,3,1,3,2,2]Output: 2Explanation:
****
The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2],
[3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1],
[3,2]` also form a valid rectangle with the same area.
This problem is similar to the first version, but the input is given as two separate arrays for x and y coordinates. We can use the same approach: for each pair of points, treat them as opposite corners, check if the other two corners exist, and ensure no other point is inside or on the border.
classSolution {
funmaxAreaRect(xCoord: IntArray, yCoord: IntArray): Int {
val n = xCoord.size
val s = mutableSetOf<Pair<Int,Int>>()
for (i in0 until n) s.add(Pair(xCoord[i], yCoord[i]))
var ans = -1for (i in0 until n) {
for (j in i+1 until n) {
val x1 = xCoord[i]; val y1 = yCoord[i]
val x2 = xCoord[j]; val y2 = yCoord[j]
if (x1 == x2 || y1 == y2) continueif (Pair(x1, y2) in s && Pair(x2, y1) in s) {
val minx = minOf(x1, x2)
val maxx = maxOf(x1, x2)
val miny = minOf(y1, y2)
val maxy = maxOf(y1, y2)
var valid = truefor (k in0 until n) {
val px = xCoord[k]; val py = yCoord[k]
if (px > minx && px < maxx && py > miny && py < maxy) {
valid = falsebreak }
}
if (valid) ans = maxOf(ans, kotlin.math.abs(x1-x2)*kotlin.math.abs(y1-y2))
}
}
}
return ans
}
}