Construct the Rectangle

easy math

Problem

Given the area of a rectangle, return [L, W] (L ≥ W) such that L × W = area, the absolute difference L − W is minimised, and the pair is as close to a square as possible.

Inputarea = 122122
Output[427, 286]
Start W = ⌊√area⌋ and walk down until area is divisible by W.

import math
def construct_rectangle(area):
    w = int(math.isqrt(area))
    while area % w != 0:
        w -= 1
    return [area // w, w]
function constructRectangle(area) {
  let w = Math.floor(Math.sqrt(area));
  while (area % w !== 0) w--;
  return [area / w, w];
}
class Solution {
    public int[] constructRectangle(int area) {
        int w = (int)Math.sqrt(area);
        while (area % w != 0) w--;
        return new int[]{ area / w, w };
    }
}
vector<int> constructRectangle(int area) {
    int w = (int)sqrt((double)area);
    while (area % w != 0) w--;
    return { area / w, w };
}
Time: O(√area) Space: O(1)