Maximum Area of Longest Diagonal Rectangle — Solution
Maximum Area of Longest Diagonal Rectangle solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Maximum Area of Longest Diagonal Rectangle is an easy coding problem on arrays, asked in Amazon and Google interviews.
- Difficulty: Easy
- Topics: Arrays
- Asked at: Amazon, Google
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given a list of rectangles, where dimensions[i] = [length, width] describes rectangle i.
Find the rectangle with the longest diagonal and return its area. If several rectangles share the longest diagonal, return the largest area among them.
Example 1
Input: dimensions = [[9,3],[8,6]]
Output: 48
Explanation: The diagonals are √90 ≈ 9.49 and √100 = 10, so the second rectangle wins with area 8 × 6 = 48.
Example 2
Input: dimensions = [[3,4],[4,3]]
Output: 12
Explanation: Both diagonals are 5 and both areas are 12.
Example 3
Input: dimensions = [[1,7],[5,5],[2,6]]
Output: 25
Explanation: 1² + 7² = 5² + 5² = 50 is the longest; between areas 7 and 25 the larger is taken.
Constraints
1 <= dimensions.length <= 100dimensions[i].length == 21 <= dimensions[i][0], dimensions[i][1] <= 100
Hints for Maximum Area of Longest Diagonal Rectangle
Hint 1
The diagonal of an
l × wrectangle issqrt(l² + w²). Do you need the square root to compare two diagonals?Hint 2
Compare
l² + w²directly — it is an integer and orders the diagonals exactly, with no floating-point ties to worry about.Hint 3
Scan once, keeping the best squared diagonal and, for ties, the best area.
How to solve Maximum Area of Longest Diagonal Rectangle
The square root is monotonic, so the longest diagonal is the one with the largest l² + w². Comparing those integers avoids floating point entirely; ties are broken by area.
Approach
- Keep
bestDiag = -1andbestArea = 0. - For each
[l, w], computed = l² + w²anda = l × w. - If
d > bestDiag, ord == bestDiaganda > bestArea, takedandaas the new best. - Return
bestArea.
Why it works
sqrt is strictly increasing on non-negative numbers, so sqrt(d1) < sqrt(d2) exactly when d1 < d2; the scan therefore ends on the largest diagonal, and the tie rule keeps the largest area among the rectangles that share it.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Comparing floating-point square roots can misjudge exact ties such as
[1,7]and[5,5]; compare the squares. - A tie on the diagonal does not imply a tie on the area — handle the tie-break explicitly.
Reference solution
Python
from typing import List
def areaOfMaxDiagonal(dimensions: List[List[int]]) -> int:
best_diag, best_area = -1, 0
for l, w in dimensions:
d, a = l * l + w * w, l * w
if d > best_diag or (d == best_diag and a > best_area):
best_diag, best_area = d, a
return best_areaJava
public static int areaOfMaxDiagonal(int[][] dimensions) {
int bestDiag = -1, bestArea = 0;
for (int[] r : dimensions) {
int d = r[0] * r[0] + r[1] * r[1], a = r[0] * r[1];
if (d > bestDiag || (d == bestDiag && a > bestArea)) { bestDiag = d; bestArea = a; }
}
return bestArea;
}C++
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
int bestDiag = -1, bestArea = 0;
for (auto& r : dimensions) {
int d = r[0] * r[0] + r[1] * r[1], a = r[0] * r[1];
if (d > bestDiag || (d == bestDiag && a > bestArea)) { bestDiag = d; bestArea = a; }
}
return bestArea;
}JavaScript
var areaOfMaxDiagonal = function(dimensions) {
var bestDiag = -1, bestArea = 0;
for (var i = 0; i < dimensions.length; i++) {
var l = dimensions[i][0], w = dimensions[i][1];
var d = l * l + w * w, a = l * w;
if (d > bestDiag || (d === bestDiag && a > bestArea)) { bestDiag = d; bestArea = a; }
}
return bestArea;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays