#3047
Medium Algorithms Find the largest area of square inside two rectangles
Array Math Geometry
66.9% acceptance
Feb 25, 2026
437
85
There exist n rectangles in a 2D plane with edges parallel to the x and y axis. You are given two 2D integer arrays bottomLeft and topRight. You need to find the maximum area of a square that can fit inside the intersecting region of at least two rectangles. Return 0 if such a square does not exist.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn largest_square_area(bottom_left: Vec<Vec<i32>>, top_right: Vec<Vec<i32>>) -> i64 {
let n = bottom_left.len();
let mut ans = 0i64;
for i in 0..n {
for j in (i+1)..n {
let lx = bottom_left[i][0].max(bottom_left[j][0]);
let ly = bottom_left[i][1].max(bottom_left[j][1]);
let rx = top_right[i][0].min(top_right[j][0]);
let ry = top_right[i][1].min(top_right[j][1]);
if rx > lx && ry > ly {
let side = (rx - lx).min(ry - ly) as i64;
ans = ans.max(side * side);
}
}
}
ans
}
}