We can rewrite as where is any arbitrary integer. So, it’s clear that will be a divisor of which is strictly greater than and the same goes for . Now to obtain maximum we’ve to take as and the biggest divisor of as , biggest divisor of which is not equal to and greater than . By doing this we can ensure because divides both and and this is the maximum that we can obtain. Now to find the biggest divisor of , we have to factorize . Let be the smallest prime factor of where , then . Note that, prime factorization in won’t fit in the time limit. So we’ve to factorize by only the prime numbers which can be found by Sieve of Eratosthenes. There are a total of primes under . So, it will easily fit in the limit.
Corner Cases: If or then the answer will be . If is a prime number and , the answer will be . If is a prime and then the answer will be .