Construct the Rectangle (LeetCode 492): Solution and Explanation

Easy
Math

Given an area, return [L, W] with L * W equal to the area, L >= W, and L - W as small as possible.

Intuition

If W <= L and L * W = area, then W * W <= area, so W is never larger than the square root of the area.

Widths closer to the square root give lengths closer to it as well, so the gap L - W shrinks as W grows. The best width is therefore the largest divisor that does not pass the square root.

That suggests counting down: start at floor(sqrt(area)) and stop at the first width that divides the area evenly. The first hit is automatically the best one.

The search always ends, because W = 1 divides every area. For a prime area that is where it ends, giving [area, 1].

Approach: Count down from the square root

  1. Set w = floor(sqrt(area)).
  2. While area % w is not 0, decrease w by one.
  3. Return [area / w, w].

Step-by-step example

area = 4
  1. floor(sqrt(4)) = 2, so start with w = 2.
  2. 4 % 2 = 0, so 2 divides the area and the loop never runs.
  3. L = 4 / 2 = 2. Return [2, 2], a perfect square with gap 0.

Code: Python, Java, C++

1class Solution:
2    def constructRectangle(self, area: int) -> List[int]:
3        w = int(area ** 0.5)
4        while area % w:
5            w -= 1
6        return [area // w, w]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(sqrt(area))

The width starts at floor(sqrt(area)) and decreases by one each step, so there are at most sqrt(area) checks. For area up to 10^7 that is about 3,162.

Space

O(1)

Only the width and the two returned numbers are stored.

Other approaches

Count up and remember the last divisor (time O(sqrt(area)), space O(1))

Try every w from 1 to sqrt(area) and keep the last one that divides the area. It gives the same answer, but always does all sqrt(area) checks instead of stopping early.

Try every length (time O(area), space O(1))

Loop L from area down to 1 and check each pair. Correct, but O(area), which is 10^7 steps in the worst case.

Common mistakes

  • Returning [W, L] instead of [L, W]. The longer side comes first.
  • Starting above the square root. With area = 12, starting at ceil(sqrt(12)) = 4 finds 12 % 4 = 0 and returns [3, 4], where W is larger than L.
  • Floating point sqrt on very large numbers can be off by one. For areas up to 10^7 the floor is exact, but integer square root is safer in general.
  • Counting up from 1 and returning the first divisor, which is always 1.

Frequently asked questions

Why is the first divisor found while counting down the best answer?

Every valid W is at most sqrt(area), and L - W = area / W - W gets smaller as W grows. The first divisor found when counting down is the largest valid W, so it has the smallest gap.

What happens when the area is a perfect square?

floor(sqrt(area)) divides the area exactly, so the loop never runs and the answer is a square with L = W.

Is there a faster way than O(sqrt(area))?

Not with a simple method. Finding the largest divisor near the square root is close to factoring, and for a prime area every candidate has to be ruled out. For this problem's limits, about 3,162 checks is tiny.