Minimum LCM of all pairs in a given array | C ++ program to find minimum possible lcm from any pair ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int minLCM ( int arr [ ] , int n ) { int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int g = gcd ( arr [ i ] , arr [ j ] ) ; int lcm = arr [ i ] / g * arr [ j ] ; ans = min ( ans , lcm ) ; } } return ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minLCM ( arr , n ) << endl ; return 0 ; }