Closest Divisors - Problem

Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2.

Return the two integers in any order.

Example: If num = 8, we need to find two integers whose product is either 9 or 10. For 9: divisors are [1,9] and [3,3]. For 10: divisors are [1,10], [2,5]. The closest pair is [3,3] with difference 0.

Input & Output

Example 1 — Perfect Square Case
$ Input: num = 8
Output: [3,3]
💡 Note: For num+1=9: divisors are [1,9] and [3,3]. For num+2=10: divisors are [1,10], [2,5]. The closest pair is [3,3] with difference 0.
Example 2 — Small Number
$ Input: num = 2
Output: [2,2]
💡 Note: For num+1=3: divisors are [1,3]. For num+2=4: divisors are [1,4], [2,2]. The closest pair is [2,2] with difference 0.
Example 3 — Prime Result
$ Input: num = 10
Output: [3,4]
💡 Note: For num+1=11: divisors are [1,11] (11 is prime). For num+2=12: divisors include [3,4]. The closest pair is [3,4] with difference 1.

Constraints

  • 1 ≤ num ≤ 109

Visualization

Tap to expand
Closest Divisors Problem: Find Pairs with Minimum DifferenceInputnum = 8Check Targetsnum+1 = 9Divisors: [1,9], [3,3]num+2 = 10Divisors: [1,10], [2,5]Output[3,3]Differences: |1-9|=8, |3-3|=0, |1-10|=9, |2-5|=3Minimum difference is 0 from pair [3,3]
Understanding the Visualization
1
Input
Given integer num = 8
2
Process
Find divisors of num+1=9 and num+2=10, choose closest pair
3
Output
Return [3,3] as it has minimum difference of 0
Key Takeaway
🎯 Key Insight: The closest divisors are always found near the square root of the target number
Asked in
Facebook 25 Amazon 20
32.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen