Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Construct the Rectangle

C++, Python and TypeScript solutions for Construct the Rectangle.

The Construct the Rectangle solutions are below.

Solution files

Python•construct-the-rectangle/synced-solution.py
def constructRectangle(area):
    w = int(area ** 0.5)

    while area % w != 0:
        w -= 1

    return [area // w, w]
C++•construct-the-rectangle/synced-solution.cpp
class Solution {
public:
    vector<int> constructRectangle(int area) {
        int w = (int)sqrt(area);

        while (area % w != 0) {
            w--;
        }

        return {area / w, w};
    }
};
TypeScript•construct-the-rectangle/synced-solution.ts
function constructRectangle(area: number): number[] {
    let w = Math.floor(Math.sqrt(area));

    while (area % w !== 0) {
        w--;
    }

    return [area / w, w];
}