Maximum Area Rectangle With Point Constraints I - Problem

You are given an array points where points[i] = [xi, yi] represents the coordinates of a point on an infinite plane.

Your task is to find the maximum area of a rectangle that:

  • Can be formed using four of these points as its corners
  • Does not contain any other point inside or on its border
  • Has its edges parallel to the axes

Return the maximum area that you can obtain or -1 if no such rectangle is possible.

Input & Output

Example 1 — Valid Rectangle Found
$ Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: -1
💡 Note: Points [1,1], [1,3], [3,1], [3,3] could form a 2×2 rectangle with area 4, but point [2,2] is inside this rectangle, making it invalid. No other valid rectangles can be formed.
Example 2 — No Valid Rectangle
$ Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: -1
💡 Note: The only potential rectangle has point [2,2] inside it, making it invalid. No other combination of 4 points can form a valid rectangle.
Example 3 — Multiple Valid Rectangles
$ Input: points = [[1,1],[1,3],[3,1],[3,3],[4,4],[4,6],[6,4],[6,6]]
Output: 4
💡 Note: Two valid rectangles: [1,1],[1,3],[3,1],[3,3] with area 4, and [4,4],[4,6],[6,4],[6,6] with area 4. Maximum area is 4.

Constraints

  • 4 ≤ points.length ≤ 3000
  • -105 ≤ xi, yi ≤ 105
  • All points are distinct

Visualization

Tap to expand
Maximum Area Rectangle With Point Constraints INPUT 1 2 3 1 2 3 1,1 1,3 3,1 3,3 2,2 Input Array: [[1,1],[1,3],[3,1],[3,3],[2,2]] 5 points on 2D plane Red point = blocker ALGORITHM STEPS 1 Sort Points Sort by x, then by y coords 2 Find Rectangle Pairs Check all pairs of points 3 Validate Corners Ensure 4 corners exist 4 Check Constraints No points inside/on border Point inside = INVALID No blocker = VALID FINAL RESULT width=2 h=2 Output: 4 Area = 2 x 2 = 4 OK - Valid rectangle No blockers inside Point (2,2) is outside this rectangle bounds Key Insight: Sort and Sweep: After sorting points, iterate through pairs to find potential diagonal corners. For each valid rectangle (4 corners exist), verify no other points lie inside or on the border. Track maximum area among all valid rectangles. Return -1 if no valid rectangle exists. TutorialsPoint - Maximum Area Rectangle With Point Constraints I | Sort and Sweep Optimization
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
8.4K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen