Rectangles Area - Problem
Given a Points table containing 2D coordinates, find all possible axis-aligned rectangles with non-zero area that can be formed by any two points.
Table Schema:
id(int): Unique identifier for each pointx_value(int): X coordinate of the pointy_value(int): Y coordinate of the point
Requirements:
- Each row should contain
p1,p2(point IDs), andarea - Only include rectangles with non-zero area
- Order by area DESC, then p1 ASC, then p2 ASC
Table Schema
Points
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Unique identifier for each point |
x_value
|
int | X coordinate of the point |
y_value
|
int | Y coordinate of the point |
Primary Key: id
Note: Each point represents a 2D coordinate (x_value, y_value)
Input & Output
Example 1 — Basic Rectangle Formation
Input Table:
| id | x_value | y_value |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 3 | 3 |
| 3 | 2 | 4 |
Output:
| p1 | p2 | area |
|---|---|---|
| 1 | 3 | 6 |
| 1 | 2 | 4 |
| 2 | 3 | 1 |
💡 Note:
Points (1,1), (3,3), and (2,4) form rectangles with areas: Point 1 to 3: |1-2|×|1-4| = 6, Point 1 to 2: |1-3|×|1-3| = 4, Point 2 to 3: |3-2|×|3-4| = 1. Results ordered by area descending.
Example 2 — Points with Same Coordinates
Input Table:
| id | x_value | y_value |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
Output:
| p1 | p2 | area |
|---|
💡 Note:
No rectangles can be formed because no two points have both different x_value AND different y_value coordinates. Points sharing the same x or y coordinate cannot form rectangles with non-zero area.
Example 3 — Single Valid Rectangle
Input Table:
| id | x_value | y_value |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 3 | 4 |
Output:
| p1 | p2 | area |
|---|---|---|
| 1 | 2 | 12 |
💡 Note:
Points (0,0) and (3,4) form one rectangle with area = |0-3| × |0-4| = 3 × 4 = 12.
Constraints
-
1 ≤ Points table rows ≤ 500 -
-10^6 ≤ x_value, y_value ≤ 10^6 -
All id values are unique
Visualization
Tap to expand
Understanding the Visualization
1
Input
Points table with coordinates
2
Self-Join
Create all point pair combinations
3
Output
Rectangle areas ordered by size
Key Takeaway
🎯 Key Insight: Use self-joins to compare all point pairs and geometric formulas to calculate areas
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code