LeetCode 3047 | Find the Largest Area of Square Inside Two Rectangles | Java | Medium | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 3047 - Find the Largest Area of Square Inside Two Rectangles using 2D Geometry and Pairwise Intersection in Java.
We are given n axis-aligned rectangles defined by their bottom-left and top-right coordinates. Our goal is to find the maximum possible area of a square that fits completely inside the intersecting region of at least two rectangles. Any square that fits inside the intersection of three or more rectangles must also fit inside the intersection of any pair among them. Therefore, finding the largest square inside any pair of rectangles guarantees the optimal global answer.
For any two rectangles i and j, their overlapping region along the x-axis starts at the maximum of their bottom-left x-coordinates and ends at the minimum of their top-right x-coordinates. Similarly, their overlapping region along the y-axis starts at the maximum of their bottom-left y-coordinates and ends at the minimum of their top-right y-coordinates. If both width and height are strictly positive, the maximum square fitting inside this intersection has side length equal to the minimum of the width and height. With n at most 1000, testing all pairs runs in O(N^2) time and O(1) auxiliary space, effortlessly handling large coordinate values up to 10^7 with 64-bit precision.
π₯ Problem Highlights
β Observation on why checking pairwise rectangle intersections is sufficient
β Axis-aligned 2D rectangle overlap formula in O(1) time per pair
β Direct calculation of maximum square side using the minimum of width and height
β 64-bit integer tracking to prevent integer overflow when squaring side lengths up to 10^7
β Clean handling when no pair of rectangles intersects, returning 0
β Optimal O(N^2) time complexity and O(1) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
01:09 Problem Understanding
02:38 Observation and 2D Overlap Decomposition
04:17 Approach - Pairwise Scanning and Inscribed Square
05:38 Code Explanation
07:07 Code Run and Dry Run
08:32 Time Complexity Analysis
09:23 Space Complexity Analysis
09:59 Outro and Summary
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
2D Plane Geometry
Rectangle Intersection Algorithms
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is it sufficient to check only pairwise rectangle intersections to find the maximum square fitting inside the intersection of at least two rectangles?
1οΈβ£ Three rectangles can never intersect in a 2D plane
2οΈβ£ Any square that fits in the intersection of two or more rectangles also fits inside the intersection of any pair among them
3οΈβ£ Rectangles can always be rotated into squares
4οΈβ£ Intersections of three rectangles always have an area of 0
#LeetCode3047 #LeetCode #FindTheLargestAreaOfSquareInsideTwoRectangles #Java #JavaDSA #Array #Geometry #Math #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 3047 - Find the Largest Area of Square Inside Two Rectangles using 2D Geometry and Pairwise Intersection in Java.
We are given n axis-aligned rectangles defined by their bottom-left and top-right coordinates. Our goal is to find the maximum possible area of a square that fits completely inside the intersecting region of at least two rectangles. Any square that fits inside the intersection of three or more rectangles must also fit inside the intersection of any pair among them. Therefore, finding the largest square inside any pair of rectangles guarantees the optimal global answer.
For any two rectangles i and j, their overlapping region along the x-axis starts at the maximum of their bottom-left x-coordinates and ends at the minimum of their top-right x-coordinates. Similarly, their overlapping region along the y-axis starts at the maximum of their bottom-left y-coordinates and ends at the minimum of their top-right y-coordinates. If both width and height are strictly positive, the maximum square fitting inside this intersection has side length equal to the minimum of the width and height. With n at most 1000, testing all pairs runs in O(N^2) time and O(1) auxiliary space, effortlessly handling large coordinate values up to 10^7 with 64-bit precision.
π₯ Problem Highlights
β Observation on why checking pairwise rectangle intersections is sufficient
β Axis-aligned 2D rectangle overlap formula in O(1) time per pair
β Direct calculation of maximum square side using the minimum of width and height
β 64-bit integer tracking to prevent integer overflow when squaring side lengths up to 10^7
β Clean handling when no pair of rectangles intersects, returning 0
β Optimal O(N^2) time complexity and O(1) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
01:09 Problem Understanding
02:38 Observation and 2D Overlap Decomposition
04:17 Approach - Pairwise Scanning and Inscribed Square
05:38 Code Explanation
07:07 Code Run and Dry Run
08:32 Time Complexity Analysis
09:23 Space Complexity Analysis
09:59 Outro and Summary
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/January/17-January/3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
2D Plane Geometry
Rectangle Intersection Algorithms
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is it sufficient to check only pairwise rectangle intersections to find the maximum square fitting inside the intersection of at least two rectangles?
1οΈβ£ Three rectangles can never intersect in a 2D plane
2οΈβ£ Any square that fits in the intersection of two or more rectangles also fits inside the intersection of any pair among them
3οΈβ£ Rectangles can always be rotated into squares
4οΈβ£ Intersections of three rectangles always have an area of 0
#LeetCode3047 #LeetCode #FindTheLargestAreaOfSquareInsideTwoRectangles #Java #JavaDSA #Array #Geometry #Math #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




