Maximum Area Rectangle With Point Constraints II - Problem

There are n points on an infinite plane. You are given two integer arrays xCoord and yCoord where (xCoord[i], yCoord[i]) represents the coordinates of the ith point.

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
$ Input: xCoord = [1,1,3,3], yCoord = [1,3,1,3]
Output: 4
💡 Note: Four points form a 2×2 rectangle with corners at (1,1), (1,3), (3,1), (3,3). No other points inside, area = 2×2 = 4
Example 2 — Point Inside Rectangle
$ Input: xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,2]
Output: -1
💡 Note: Point (2,2) lies inside any rectangle formed by the corner points, making no valid rectangle possible
Example 3 — Multiple Valid Rectangles
$ Input: xCoord = [1,1,3,3,5,5], yCoord = [1,3,1,3,1,3]
Output: 8
💡 Note: Two rectangles possible: (1,1)-(3,3) with area 4, and (3,1)-(5,3) with area 4. Maximum area is 4, but (1,1)-(5,3) gives area 4×2=8

Constraints

  • 1 ≤ xCoord.length ≤ 2000
  • xCoord.length == yCoord.length
  • -105 ≤ xCoord[i], yCoord[i] ≤ 105
  • All points are unique

Visualization

Tap to expand
INPUTALGORITHMRESULTPoint Coordinates(1,1)(3,1)(1,3)(3,3)(2,2)xCoord: [1,1,3,3,2]yCoord: [1,3,1,3,2]1Try all 4-point combinations2Check rectangle formation3Verify no internal points4Calculate maximum areaPoint (2,2) blocks rectangleformation → No valid rectangleOutput Value-1No valid rectanglecan be formedInternal point (2,2)prevents any rectanglefrom being validKey Insight:A valid rectangle requires exactly 4 corner points with no other points insideor on the boundary. Any internal point makes the rectangle invalid.TutorialsPoint - Maximum Area Rectangle With Point Constraints II | Coordinate Geometry
Asked in
Google 45 Microsoft 32 Amazon 28 Meta 22
12.4K Views
Medium Frequency
~35 min Avg. Time
234 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