Maximum possible GCD for a pair of integers with product N | Java program to implement the above approach ; Function to return the maximum GCD ; To find all divisors of N ; If i is a factor ; Store the pair of factors ; Store the maximum GCD ; Return the maximum GCD ; Driver Code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int getMaxGcd ( int N ) { int maxGcd = Integer . MIN_VALUE , A , B ; for ( int i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { A = i ; B = N / i ; maxGcd = Math . max ( maxGcd , gcd ( A , B ) ) ; } } return maxGcd ; } public static void main ( String s [ ] ) { int N = 18 ; System . out . println ( getMaxGcd ( N ) ) ; } }