VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-two-numbers-with-the-given-lcm-and-minimum-possible-difference/

⇱ Find two numbers with the given LCM and minimum possible difference - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find two numbers with the given LCM and minimum possible difference

Last Updated : 12 Jul, 2025

Given an integer X, the task is to find two integers A and B such that LCM(A, B) = X and the difference between the A and B is minimum possible.
Examples: 
 

Input: X = 6 
Output: 2 3 
LCM(2, 3) = 6 and (3 - 2) = 1 
which is the minimum possible.
Input X = 7 
Output: 1 7 
 


 


Approach: An approach to solve this problem is to find all the factors of the given number using the approach discussed in this article and then find the pair (A, B) that satisfies the given conditions and has the minimum possible difference.
Below is the implementation of the above approach: 
 


Output: 
3 4

 

Time Complexity: O(n1/2 * log(max(a, b)))

Auxiliary Space: O(log(max(a, b)))

Comment
Article Tags: