Maximum Area of Longest Diagonal Rectangle

Easy
1842.

Maximum Area of Longest Diagonal Rectangle

Maximum Area of Longest Diagonal Rectangle is an easy coding problem on arrays, asked in Amazon and Google interviews.

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 <= 100
  • dimensions[i].length == 2
  • 1 <= dimensions[i][0], dimensions[i][1] <= 100
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[][]} dimensions
 * @return {number}
 */
var areaOfMaxDiagonal = function(dimensions) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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 <= 100
  • dimensions[i].length == 2
  • 1 <= dimensions[i][0], dimensions[i][1] <= 100

Hints for Maximum Area of Longest Diagonal Rectangle

  1. Hint 1

    The diagonal of an l × w rectangle is sqrt(l² + w²). Do you need the square root to compare two diagonals?

  2. Hint 2

    Compare l² + w² directly — it is an integer and orders the diagonals exactly, with no floating-point ties to worry about.

  3. 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

  1. Keep bestDiag = -1 and bestArea = 0.
  2. For each [l, w], compute d = l² + w² and a = l × w.
  3. If d > bestDiag, or d == bestDiag and a > bestArea, take d and a as the new best.
  4. 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_area

Java

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