Find the maximum GCD possible for some pair in a given range [ L , R ] | Function to calculate GCD ; Function to calculate maximum GCD in a range ; Variable to store the answer ; If Z has two multiples in [ L , R ] ; Update ans ; Return the value ; Input ; Function Call
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function maxGCDInRange ( L , R ) { let ans = 1 ; for ( let Z = R ; Z >= 1 ; Z -- ) { if ( parseInt ( ( R / Z ) ) - parseInt ( ( L - 1 ) / Z ) > 1 ) { ans = Z ; break ; } } return ans ; } let L = 102 ; let R = 139 ; document . write ( maxGCDInRange ( L , R ) ) ;