Leetcode

Construct the Rectangle

  • Time:O(n)
  • Space:O(1)

C++

class Solution {
 public:
  vector<int> constructRectangle(int area) {
    int width = sqrt(area);

    while (area % width)
      --width;

    return {area / width, width};
  }
};

JAVA

class Solution {
  public int[] constructRectangle(int area) {
    int width = (int) Math.sqrt(area);

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

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