Pair of integers having least GCD among all given pairs having GCD exceeding K | Function to calculate the GCD of two numbers ; Function to print the pair having a gcd value just greater than the given integer ; Initialize variables ; Iterate until low less than equal to high ; Calculate mid ; Reducing the search space ; Print the answer ; Given array and K ; Function Call
function GCD ( a , b ) { if ( b == 0 ) { return a ; } return GCD ( b , a % b ) ; } function GcdPair ( arr , k ) { var lo = 0 , hi = arr . length - 1 , mid ; var ans = [ - 1 , 0 ] ; while ( lo <= hi ) { mid = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( GCD ( arr [ mid ] [ 0 ] , arr [ mid ] [ 1 ] ) > k ) { ans = arr [ mid ] ; hi = mid - 1 ; } else lo = mid + 1 ; } if ( ans [ 0 ] == - 1 ) document . write ( " " ) ; else document . write ( " " + ans [ 0 ] + " " + ans [ 1 ] + " " ) ; return ; } var arr = [ [ 3 , 6 ] , [ 15 , 30 ] , [ 25 , 75 ] , [ 30 , 120 ] ] ; var K = 16 ; GcdPair ( arr , K ) ;