Find the Largest Area of Square Inside Two Rectangles

Medium
Watch on YouTube ↗

Solution

class Solution {

    public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
        int n = bottomLeft.length;
        long maxSide = 0;

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int left = Math.max(bottomLeft[i][0], bottomLeft[j][0]);
                int right = Math.min(topRight[i][0], topRight[j][0]);

                int width = right-left;

                int bottom = Math.max(bottomLeft[i][1], bottomLeft[j][1]);
                int top = Math.min(topRight[i][1], topRight[j][1]);

                int length = top-bottom;
                    
                maxSide = Math.max(maxSide, Math.min(length, width));
            }
        }

        return maxSide * maxSide;
    }
}