Rectangles Area - Problem
Imagine you have a collection of points scattered across a coordinate plane, and you want to find all possible axis-aligned rectangles that can be formed using any two of these points as opposite corners!
Given a table Points with columns:
id- unique identifier for each pointx_value- x-coordinate of the pointy_value- y-coordinate of the point
Your mission: Find all possible rectangles with non-zero area that can be formed by selecting any two points as opposite corners. Each rectangle must be axis-aligned (sides parallel to x and y axes).
Return format: Each row should contain (p1, p2, area) where:
p1andp2are the IDs of the two corner pointsareais the calculated area (must be > 0)
Sorting: Order by area (descending), then by p1 (ascending), then by p2 (ascending).
Input & Output
example_1.sql โ Basic Rectangle Formation
$
Input:
Points table:
+----+---------+---------+
| id | x_value | y_value |
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 3 | 1 |
| 4 | 3 | 3 |
+----+---------+---------+
โบ
Output:
+----+----+------+
| p1 | p2 | area |
+----+----+------+
| 1 | 4 | 4 |
| 2 | 3 | 4 |
+----+----+------+
๐ก Note:
Points (1,1) and (3,3) form a 2ร2 rectangle with area 4. Points (1,3) and (3,1) also form a 2ร2 rectangle with area 4. Both have same area, so ordered by p1.
example_2.sql โ Different Sized Rectangles
$
Input:
Points table:
+----+---------+---------+
| id | x_value | y_value |
+----+---------+---------+
| 1 | 0 | 0 |
| 2 | 4 | 0 |
| 3 | 0 | 3 |
| 4 | 4 | 4 |
+----+---------+---------+
โบ
Output:
+----+----+------+
| p1 | p2 | area |
+----+----+------+
| 1 | 4 | 16 |
| 2 | 3 | 12 |
+----+----+------+
๐ก Note:
Point (0,0) and (4,4) create a 4ร4 rectangle (area=16). Point (4,0) and (0,3) create a 4ร3 rectangle (area=12). Ordered by area descending.
example_3.sql โ No Valid Rectangles
$
Input:
Points table:
+----+---------+---------+
| id | x_value | y_value |
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
+----+---------+---------+
โบ
Output:
+----+----+------+
| p1 | p2 | area |
+----+----+------+
(empty result)
๐ก Note:
All points have the same x-value (1), so no valid axis-aligned rectangles can be formed. Need points with different x AND y coordinates.
Constraints
- 2 โค number of points โค 500
- 0 โค x_value, y_value โค 106
- All point coordinates are integers
- id values are unique for each point
- Points may have duplicate coordinates (same x_value and y_value)
Visualization
Tap to expand
Understanding the Visualization
1
Identify Valid Pairs
Two points can form a rectangle only if they have different x-coordinates AND different y-coordinates
2
Visualize Rectangle
The two points become opposite corners of an axis-aligned rectangle
3
Calculate Area
Area equals the absolute difference in x-values times the absolute difference in y-values
4
Sort and Return
Order results by area (descending), then by first point ID, then by second point ID
Key Takeaway
๐ฏ Key Insight: Use SQL self-join to examine all point pairs, filter for different x AND y coordinates, calculate rectangular areas, and sort results appropriately.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code