Minimum LCM of all pairs in a given array | function to compute GCD of two numbers ; function that return minimum possible lcm from any pair ; fix the ith element and iterate over all the array to find minimum LCM ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function minLCM ( arr , n ) { let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let g = gcd ( arr [ i ] , arr [ j ] ) ; let lcm = arr [ i ] / g * arr [ j ] ; ans = Math . min ( ans , lcm ) ; } } return ans ; } let arr = [ 2 , 4 , 3 , 6 , 5 ] ; let n = arr . length ; document . write ( minLCM ( arr , n ) ) ;