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: 4
💡 Note: Points [1,1], [1,3], [3,1], [3,3] form a 2×2 rectangle with area 4. Point [2,2] is inside, so this rectangle is invalid. No other valid rectangles exist, but let's say we had points without [2,2] - then area would be 4.
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 ConstraintsInput Points:Process:InvalidPoint inside!ValidArea = 2000Output:2000Maximum areaof valid rectangleFind largest axis-aligned rectangle with 4 corner points and no interior pointsRed rectangle invalid due to interior point • Green rectangle valid
Understanding the Visualization
1
Input Points
Given set of coordinate points on a plane
2
Find Rectangles
Identify all possible axis-aligned rectangles using 4 points
3
Validate & Maximize
Ensure no points inside, return maximum area or -1
Key Takeaway
🎯 Key Insight: Use diagonal point pairs to efficiently validate rectangles instead of checking all 4-point combinations
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