Maximum Area of Longest Diagonal Rectangle - Problem

You're tasked with finding the largest area among rectangles that have the longest diagonal. Imagine you have a collection of rectangular frames, and you want to pick the one with the greatest diagonal length - but if multiple frames tie for the longest diagonal, you'll choose the one with the biggest area instead.

Given a 2D array dimensions where dimensions[i] = [length, width] represents the dimensions of rectangle i, your goal is to:

  1. Calculate the diagonal length for each rectangle using the Pythagorean theorem: √(length² + width²)
  2. Find all rectangles with the maximum diagonal length
  3. Among those rectangles, return the area of the one with the largest area

Example: If you have rectangles [[9,3], [8,6]], the first has diagonal √(81+9) = √90 ≈ 9.49 and the second has diagonal √(64+36) = 10. Since 10 > 9.49, return the area of the second rectangle: 8 × 6 = 48.

Input & Output

example_1.py — Basic Case
$ Input: dimensions = [[9,3],[8,6]]
Output: 48
💡 Note: Rectangle [9,3] has diagonal √(81+9) = √90 ≈ 9.49 and area 27. Rectangle [8,6] has diagonal √(64+36) = 10 and area 48. Since 10 > 9.49, we return 48.
example_2.py — Tie in Diagonal
$ Input: dimensions = [[3,4],[4,3],[3,4]]
Output: 12
💡 Note: All rectangles have diagonal √(9+16) = 5 and area 12. Since they all tie for longest diagonal, we return the maximum area among them, which is 12.
example_3.py — Single Rectangle
$ Input: dimensions = [[5,12]]
Output: 60
💡 Note: Only one rectangle with diagonal √(25+144) = 13 and area 60. Return 60.

Constraints

  • 1 ≤ dimensions.length ≤ 100
  • dimensions[i].length == 2
  • 1 ≤ dimensions[i][0], dimensions[i][1] ≤ 100
  • No two rectangles have the same dimensions

Visualization

Tap to expand
Finding Maximum Area with Longest Diagonal[9,3]d=√90≈9.49Area=27[8,8]d=√128≈11.31Area=64[6,10]d=√136≈11.66Area=60[7,8]d=√113≈10.63Area=56Winner: [6,10] with longest diagonal √136 and area 60Algorithm: Track max diagonal and corresponding max area in one passTime: O(n), Space: O(1)
Understanding the Visualization
1
Measure Each Frame
Use Pythagorean theorem to calculate diagonal: √(length² + width²)
2
Track the Champion
Keep track of the current longest diagonal and the largest area among frames with that diagonal
3
Update When Better Found
When you find a longer diagonal, that frame becomes the new champion. If diagonal ties, pick the one with bigger area
4
Return Final Champion
After checking all frames, return the area of the champion frame
Key Takeaway
🎯 Key Insight: We only need to track the current maximum diagonal and the maximum area among rectangles with that diagonal - no need to store all calculations!
Asked in
Amazon 15 Microsoft 8 Google 6 Apple 4
28.4K Views
Medium Frequency
~12 min Avg. Time
892 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