# Solving Leetcode Interviews in Seconds with AI: Closest Divisors


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "1362" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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 1:  Input: num = 8 Output: [3,3] Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen.  Example 2:  Input: num = 123 Output: [5,25]  Example 3:  Input: num = 999 Output: [40,25]    Constraints:  1 <= num <= 10^9  

	# Explanation
	Here's a solution to find the closest factors whose product is `num + 1` or `num + 2`:

*   **Core Idea**: Iterate downwards from the square root of `num + 2` (the larger possible product) to find a factor. Once a factor is found for either `num + 1` or `num + 2`, calculate the corresponding pair, and return. Starting from the square root ensures that we find the closest factors quickly.

*   **Prioritization**: Check for factors of `num + 1` before `num + 2`. If both have equally close factors, the problem statement implies choosing factors of `num+1`

*   **Optimization**: Iterating downwards avoids calculating square roots repeatedly and ensures the first factor found is from the closest factor pair.

*   **Complexity**: O(sqrt(n)) runtime and O(1) space complexity, where n is the input number `num`.

	
	# Code
	```python
	def closestDivisors(num: int) -> list[int]:
    """
    Finds the closest two integers in absolute difference whose product equals num + 1 or num + 2.

    Args:
        num: The input integer.

    Returns:
        A list of two integers representing the closest divisors.
    """

    best_a = 1
    best_b = num + 2
    min_diff = num + 1  # Initialize with a large difference

    for i in range(int((num + 2)**0.5), 0, -1):
        if (num + 1) % i == 0:
            a = i
            b = (num + 1) // i
            diff = abs(a - b)
            if diff < min_diff:
                min_diff = diff
                best_a = a
                best_b = b
            break  # Found factors for num + 1, no need to check num + 2 further if they're equally close

        if (num + 2) % i == 0:
            a = i
            b = (num + 2) // i
            diff = abs(a - b)
            if diff <= min_diff:  # Include equal because num+1 case is preferred
                min_diff = diff
                best_a = a
                best_b = b

    return [best_a, best_b]
	```
			
