Skip to content

492. Construct the Rectangle

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  vector<int> constructRectangle(int area) {
    int width = sqrt(area);

    while (area % width)
      --width;

    return {area / width, width};
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
  public int[] constructRectangle(int area) {
    int width = (int) Math.sqrt(area);

    while (area % width > 0)
      --width;

    return new int[] {area / width, width};
  }
}