Ctrl K

Tightest Factor Pair

Medium
You need to lay out a rectangle of total + 1 or total + 2 identical tiles, and it should be as close to a square as possible. Find two positive integers whose product is total + 1 or total + 2 and whose difference is as small as possible. Return the two numbers in any order.
Example 1
total=13 → [3, 5]

14 = 2 × 7 (difference 5), 15 = 3 × 5 (difference 2). The tighter pair is 3 and 5.

Example 2
total=24 → [5, 5]

25 = 5 × 5, a difference of 0, which cannot be beaten.

Example 3
total=5 → [2, 3]

6 = 2 × 3 (difference 1), while 7 is prime and only splits as 1 × 7. The answer is 2 and 3.

Constraints
  • 1 ≤ total ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumBackwards Digits in 32 Bits MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
total=13
Expected
[3, 5]

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