Maximum Area of Longest Diagonal Rectangle - Problem

You are given a 2D 0-indexed integer array dimensions. For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.

Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area.

Input & Output

Example 1 — Basic Case
$ Input: dimensions = [[9,5],[4,6]]
Output: 45
💡 Note: Rectangle 1: diagonal = √(9² + 5²) = √106 ≈ 10.3, area = 45. Rectangle 2: diagonal = √(4² + 6²) = √52 ≈ 7.2, area = 24. Rectangle 1 has the longest diagonal, so return its area 45.
Example 2 — Tie in Diagonal
$ Input: dimensions = [[3,4],[4,3],[5,0]]
Output: 12
💡 Note: Rectangle 1: diagonal = √(3² + 4²) = 5, area = 12. Rectangle 2: diagonal = √(4² + 3²) = 5, area = 12. Rectangle 3: diagonal = √(5² + 0²) = 5, area = 0. All have same diagonal (5), so return maximum area which is 12.
Example 3 — Single Rectangle
$ Input: dimensions = [[6,8]]
Output: 48
💡 Note: Only one rectangle with diagonal = √(6² + 8²) = 10 and area = 48. Return 48.

Constraints

  • 1 ≤ dimensions.length ≤ 100
  • dimensions[i].length == 2
  • 1 ≤ dimensions[i][0], dimensions[i][1] ≤ 100

Visualization

Tap to expand
Maximum Area of Longest Diagonal Rectangle INPUT dimensions = [[9,5],[4,6]] 9 x 5 Rectangle 0 4 x 6 Rectangle 1 d = sqrt(9^2+5^2) = sqrt(106) d = sqrt(4^2+6^2) = sqrt(52) Input Values: Rect 0: length=9, width=5 Rect 1: length=4, width=6 ALGORITHM STEPS 1 Initialize Tracking maxDiagSq = 0, maxArea = 0 2 Process Rect 0 (9x5) diagSq = 81+25 = 106 area = 45 3 Update Best 106 > 0: Update! maxDiagSq=106, maxArea=45 4 Process Rect 1 (4x6) diagSq = 16+36 = 52 52 < 106: No update Comparison Summary Rect DiagSq Area 0 106 45 1 52 24 FINAL RESULT 9 x 5 Longest Diagonal! OK Output: 45 Why Rectangle 0? Diagonal^2 = 106 (larger than 52) Area = 9 * 5 = 45 Key Insight: Single Pass Optimization Instead of computing sqrt() for diagonals, compare diagonal^2 values directly (avoid floating point). For each rectangle: diagSq = length^2 + width^2. Track the maximum diagonal squared and its area. If tied diagonal, keep the larger area. Time: O(n), Space: O(1). TutorialsPoint - Maximum Area of Longest Diagonal Rectangle | Single Pass Optimization
Asked in
Microsoft 15 Apple 12
12.0K Views
Medium Frequency
~8 min Avg. Time
450 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