A solar-farm technician must mount a batch of solar panels into a rectangular array (some number of rows times some number of columns, one panel per grid cell, no gaps). The technician has a required panel count num, but the pre-cut mounting rails only ever produce a total panel count of exactly num + 1 or exactly num + 2 (never num itself), so up to two filler panels beyond the requirement must be used. Among every valid rectangular layout using exactly num + 1 or exactly num + 2 panels, the technician wants the layout that is closest to square, i.e. the one that minimizes the difference between the number of columns and the number of rows. If more than one layout achieves that same minimum difference, prefer the layout that uses fewer total panels (i.e. prefer the num + 1 layout over the num + 2 layout).
A single line containing one integer num (1 <= num <= 10^9).
Print two integers a b separated by a single space, where a is the number of rows, b is the number of columns, a <= b, a * b equals num + 1 or num + 2, and b - a is as small as possible (ties broken toward the smaller product, as described above).
Example 1
Input
8
Expected
3 3
Explanation
num+1 = 9 can be laid out as 3 rows by 3 columns (difference 0). num+2 = 10 can be laid out as 2 rows by 5 columns at best (difference 3). The 9-panel layout is closer to square, so the answer is `3 3`.
Example 2
Input
13
Expected
3 5
Explanation
num+1 = 14 can be laid out as 2 rows by 7 columns at best (difference 5). num+2 = 15 can be laid out as 3 rows by 5 columns (difference 2), which is closer to square, so the answer is `3 5`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →