Construct the Rectangle - Problem
A web developer needs to know how to design a web page's size. So, given a specific rectangular web page's area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
- The area of the rectangular web page you designed must equal to the given target area.
- The width W should not be larger than the length L, which means
L >= W. - The difference between length L and width W should be as small as possible.
Return an array [L, W] where L and W are the length and width of the web page you designed in sequence.
Input & Output
Example 1 — Perfect Square Area
$
Input:
area = 4
›
Output:
[2,2]
💡 Note:
Area 4 can form a 2×2 square, which has the minimum possible difference of 0
Example 2 — Non-Square Area
$
Input:
area = 6
›
Output:
[3,2]
💡 Note:
Area 6 has factors: 1×6 (diff=5) and 2×3 (diff=1). The 3×2 rectangle has minimum difference
Example 3 — Prime Number Area
$
Input:
area = 7
›
Output:
[7,1]
💡 Note:
Area 7 is prime, so only possible rectangle is 7×1
Constraints
- 1 ≤ area ≤ 107
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given area = 12
2
Process
Find factors closest to square
3
Output
Return [4, 3] rectangle
Key Takeaway
🎯 Key Insight: Start from the square root and work backwards to find the largest valid width ≤ √area
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code