Ctrl K

The Squarest Rectangle

Easy
You have tiles identical square tiles and must lay all of them out as one solid rectangle. You want it as close to a square as possible, so the difference between its length and width should be as small as it can be. Return [length, width] in tiles, with length ≥ width.
Example 1
tiles=24 → [6, 4]

The options are 24×1, 12×2, 8×3 and 6×4. The sides of 6×4 are the closest.

Example 2
tiles=49 → [7, 7]

49 = 7×7, a perfect square.

Example 3
tiles=13 → [13, 1]

13 is prime, so the only option is a 13×1 strip.

Constraints
  • 1 ≤ tiles ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Number That Reads Both Ways EasyValue of a Roman Numeral EasyAdd One to a Digit Array EasySum of Binary Strings EasyWhole Square Root Easy
loading Python…
Input
tiles=24
Expected
[6, 4]

Run checks the first 3 tests in your browser. Submit checks all 62 tests on the server, hidden ones included.