Maximum Area Rectangle With Point Constraints II - Problem

Imagine you're a city planner working with a grid-based map where certain locations are marked as special landmarks. Your task is to find the largest rectangular park that can be built using exactly four of these landmarks as corners.

Given n points on an infinite 2D plane with coordinates (xCoord[i], yCoord[i]), you need to find the maximum area rectangle that:

  • Uses exactly four points as its corners
  • Has edges parallel to the axes (axis-aligned)
  • Contains no other points inside or on its borders

Return the maximum area possible, or -1 if no valid rectangle can be formed.

Example: With points [(1,1), (1,3), (3,1), (3,3), (2,2)], the rectangle with corners (1,1), (1,3), (3,1), (3,3) would be invalid because (2,2) lies inside it.

Input & Output

Time & Space Complexity

Time Complexity
⏱️
O(n^5)

O(n^4) to check all 4-point combinations, O(n) to verify each rectangle is empty

n
2n
Linear Growth
Space Complexity
O(1)

Only storing constant amount of variables

n
2n
Linear Space

Constraints

Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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